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