| 1 |
"""Test YAML configuration with lists of dataclasses and TypedDict. |
| 2 |
|
| 3 |
This addresses GitHub issue #507. |
| 4 |
""" |
| 5 |
|
| 6 |
from dataclasses import dataclass |
| 7 |
from textwrap import dedent |
| 8 |
from typing import Literal, TypedDict |
| 9 |
|
| 10 |
from cyclopts.config import Yaml |
| 11 |
|
| 12 |
|
| 13 |
@dataclass |
| 14 |
class User: |
| 15 |
name: str |
| 16 |
age: int |
| 17 |
region: Literal["us", "ca"] = "us" |
| 18 |
|
| 19 |
|
| 20 |
class Config(TypedDict): |
| 21 |
name: str |
| 22 |
value: int |
| 23 |
|
| 24 |
|
| 25 |
def test_yaml_list_of_dataclasses(app, tmp_path, assert_parse_args): |
| 26 |
"""Test the exact scenario from GitHub issue #507 with YAML config.""" |
| 27 |
config_fn = tmp_path / "config.yaml" |
| 28 |
|
| 29 |
config_fn.write_text( |
| 30 |
dedent( |
| 31 |
"""\ |
| 32 |
users: |
| 33 |
- name: alice |
| 34 |
age: 22 |
| 35 |
region: us |
| 36 |
- name: bob |
| 37 |
age: 33 |
| 38 |
region: ca |
| 39 |
""" |
| 40 |
) |
| 41 |
) |
| 42 |
|
| 43 |
app.config = Yaml(config_fn) |
| 44 |
|
| 45 |
@app.default |
| 46 |
def main(users: list[User]): |
| 47 |
pass |
| 48 |
|
| 49 |
|
| 50 |
assert_parse_args(main, "", [User("alice", 22, "us"), User("bob", 33, "ca")]) |
| 51 |
|
| 52 |
|
| 53 |
def test_yaml_list_of_dataclasses_with_cli_override(app, tmp_path, assert_parse_args): |
| 54 |
"""Test that CLI arguments override YAML config for list of dataclasses.""" |
| 55 |
config_fn = tmp_path / "config.yaml" |
| 56 |
|
| 57 |
config_fn.write_text( |
| 58 |
dedent( |
| 59 |
"""\ |
| 60 |
users: |
| 61 |
- name: alice |
| 62 |
age: 22 |
| 63 |
region: us |
| 64 |
""" |
| 65 |
) |
| 66 |
) |
| 67 |
|
| 68 |
app.config = Yaml(config_fn) |
| 69 |
|
| 70 |
@app.default |
| 71 |
def main(users: list[User]): |
| 72 |
pass |
| 73 |
|
| 74 |
|
| 75 |
assert_parse_args( |
| 76 |
main, |
| 77 |
'--users \'{"name": "charlie", "age": 40, "region": "ca"}\'', |
| 78 |
[User("charlie", 40, "ca")], |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
def test_yaml_empty_list_of_dataclasses(app, tmp_path, assert_parse_args): |
| 83 |
"""Test empty list of dataclasses from YAML.""" |
| 84 |
config_fn = tmp_path / "config.yaml" |
| 85 |
|
| 86 |
config_fn.write_text( |
| 87 |
dedent( |
| 88 |
"""\ |
| 89 |
users: [] |
| 90 |
""" |
| 91 |
) |
| 92 |
) |
| 93 |
|
| 94 |
app.config = Yaml(config_fn) |
| 95 |
|
| 96 |
@app.default |
| 97 |
def main(users: list[User] = None): |
| 98 |
pass |
| 99 |
|
| 100 |
assert_parse_args(main, "", []) |
| 101 |
|
| 102 |
|
| 103 |
def test_yaml_single_dataclass_in_list(app, tmp_path, assert_parse_args): |
| 104 |
"""Test single dataclass item in YAML list.""" |
| 105 |
config_fn = tmp_path / "config.yaml" |
| 106 |
|
| 107 |
config_fn.write_text( |
| 108 |
dedent( |
| 109 |
"""\ |
| 110 |
users: |
| 111 |
- name: alice |
| 112 |
age: 30 |
| 113 |
region: us |
| 114 |
""" |
| 115 |
) |
| 116 |
) |
| 117 |
|
| 118 |
app.config = Yaml(config_fn) |
| 119 |
|
| 120 |
@app.default |
| 121 |
def main(users: list[User]): |
| 122 |
pass |
| 123 |
|
| 124 |
assert_parse_args(main, "", [User("alice", 30, "us")]) |
| 125 |
|
| 126 |
|
| 127 |
def test_yaml_list_of_typeddict(app, tmp_path, assert_parse_args): |
| 128 |
"""Test list of TypedDict from YAML config.""" |
| 129 |
config_fn = tmp_path / "config.yaml" |
| 130 |
|
| 131 |
config_fn.write_text( |
| 132 |
dedent( |
| 133 |
"""\ |
| 134 |
configs: |
| 135 |
- name: config1 |
| 136 |
value: 10 |
| 137 |
- name: config2 |
| 138 |
value: 20 |
| 139 |
- name: config3 |
| 140 |
value: 30 |
| 141 |
""" |
| 142 |
) |
| 143 |
) |
| 144 |
|
| 145 |
app.config = Yaml(config_fn) |
| 146 |
|
| 147 |
@app.default |
| 148 |
def main(configs: list[Config]): |
| 149 |
pass |
| 150 |
|
| 151 |
assert_parse_args( |
| 152 |
main, |
| 153 |
"", |
| 154 |
[ |
| 155 |
{"name": "config1", "value": 10}, |
| 156 |
{"name": "config2", "value": 20}, |
| 157 |
{"name": "config3", "value": 30}, |
| 158 |
], |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def test_yaml_nested_dataclass_structure(app, tmp_path, assert_parse_args): |
| 163 |
"""Test nested dataclass structure from YAML.""" |
| 164 |
|
| 165 |
@dataclass |
| 166 |
class Address: |
| 167 |
street: str |
| 168 |
city: str |
| 169 |
country: str = "US" |
| 170 |
|
| 171 |
@dataclass |
| 172 |
class Person: |
| 173 |
name: str |
| 174 |
age: int |
| 175 |
address: Address |
| 176 |
|
| 177 |
config_fn = tmp_path / "config.yaml" |
| 178 |
|
| 179 |
config_fn.write_text( |
| 180 |
dedent( |
| 181 |
"""\ |
| 182 |
people: |
| 183 |
- name: alice |
| 184 |
age: 25 |
| 185 |
address: |
| 186 |
street: 123 Main St |
| 187 |
city: New York |
| 188 |
country: US |
| 189 |
- name: bob |
| 190 |
age: 30 |
| 191 |
address: |
| 192 |
street: 456 Oak Ave |
| 193 |
city: Toronto |
| 194 |
country: CA |
| 195 |
""" |
| 196 |
) |
| 197 |
) |
| 198 |
|
| 199 |
app.config = Yaml(config_fn) |
| 200 |
|
| 201 |
@app.default |
| 202 |
def main(people: list[Person]): |
| 203 |
pass |
| 204 |
|
| 205 |
expected = [ |
| 206 |
Person("alice", 25, Address("123 Main St", "New York", "US")), |
| 207 |
Person("bob", 30, Address("456 Oak Ave", "Toronto", "CA")), |
| 208 |
] |
| 209 |
|
| 210 |
assert_parse_args(main, "", expected) |
| 211 |
|
| 212 |
|
| 213 |
def test_yaml_mixed_config_and_cli(app, tmp_path, assert_parse_args): |
| 214 |
"""Test mixing YAML config with additional CLI arguments.""" |
| 215 |
config_fn = tmp_path / "config.yaml" |
| 216 |
|
| 217 |
config_fn.write_text( |
| 218 |
dedent( |
| 219 |
"""\ |
| 220 |
users: |
| 221 |
- name: alice |
| 222 |
age: 22 |
| 223 |
region: us |
| 224 |
""" |
| 225 |
) |
| 226 |
) |
| 227 |
|
| 228 |
app.config = Yaml(config_fn) |
| 229 |
|
| 230 |
@app.default |
| 231 |
def main(users: list[User], verbose: bool = False): |
| 232 |
pass |
| 233 |
|
| 234 |
|
| 235 |
assert_parse_args(main, "--verbose", users=[User("alice", 22, "us")], verbose=True) |