| 1 |
"""Test JSON parsing behavior with special string types. |
| 2 |
|
| 3 |
This module tests JSON parsing with: |
| 4 |
- String subclasses (CustomStr) |
| 5 |
- NewType wrapping str |
| 6 |
- Union types containing str |
| 7 |
- Optional[str] (which is Union[str, None]) |
| 8 |
|
| 9 |
Key principle: If a type can accept str, then a JSON string is already |
| 10 |
a valid value for that type, so JSON parsing should be disabled. |
| 11 |
""" |
| 12 |
|
| 13 |
from dataclasses import dataclass |
| 14 |
from typing import Annotated, NewType |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from cyclopts import Parameter |
| 19 |
|
| 20 |
|
| 21 |
class CustomStr(str): |
| 22 |
"""A custom string subclass.""" |
| 23 |
|
| 24 |
|
| 25 |
MyStr = NewType("MyStr", str) |
| 26 |
|
| 27 |
|
| 28 |
@dataclass |
| 29 |
class User: |
| 30 |
name: str |
| 31 |
age: int |
| 32 |
|
| 33 |
|
| 34 |
@dataclass |
| 35 |
class UserWithCustomStr: |
| 36 |
name: CustomStr |
| 37 |
age: int |
| 38 |
|
| 39 |
|
| 40 |
@dataclass |
| 41 |
class UserWithNewType: |
| 42 |
name: MyStr |
| 43 |
age: int |
| 44 |
|
| 45 |
|
| 46 |
@dataclass |
| 47 |
class UserWithUnion: |
| 48 |
name: str | int |
| 49 |
age: int |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.parametrize( |
| 58 |
"type_hint,expected_value", |
| 59 |
[ |
| 60 |
(CustomStr, CustomStr('{"name": "Alice"}')), |
| 61 |
(MyStr, MyStr('{"name": "Alice"}')), |
| 62 |
], |
| 63 |
ids=["CustomStr", "NewType"], |
| 64 |
) |
| 65 |
def test_json_not_parsed_for_str_like_types(app, assert_parse_args, type_hint, expected_value): |
| 66 |
"""JSON should NOT be parsed for string subclasses or NewType(str).""" |
| 67 |
|
| 68 |
@app.default |
| 69 |
def main(value: type_hint): |
| 70 |
pass |
| 71 |
|
| 72 |
assert_parse_args( |
| 73 |
main, |
| 74 |
["--value", '{"name": "Alice"}'], |
| 75 |
expected_value, |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.parametrize( |
| 80 |
"type_hint,expected_value", |
| 81 |
[ |
| 82 |
(CustomStr, CustomStr('{"name": "Alice"}')), |
| 83 |
(MyStr, MyStr('{"name": "Alice"}')), |
| 84 |
], |
| 85 |
ids=["CustomStr", "NewType"], |
| 86 |
) |
| 87 |
def test_json_not_parsed_for_str_like_types_env_var(app, assert_parse_args, type_hint, expected_value, monkeypatch): |
| 88 |
"""JSON should NOT be parsed for string subclasses or NewType(str) from environment variables.""" |
| 89 |
monkeypatch.setenv("VALUE", '{"name": "Alice"}') |
| 90 |
|
| 91 |
@app.default |
| 92 |
def main(value: Annotated[type_hint, Parameter(env_var="VALUE")]): |
| 93 |
pass |
| 94 |
|
| 95 |
assert_parse_args(main, "", expected_value) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.parametrize( |
| 99 |
"type_hint,wrapper", |
| 100 |
[ |
| 101 |
(list[CustomStr], CustomStr), |
| 102 |
(list[MyStr], MyStr), |
| 103 |
], |
| 104 |
ids=["list[CustomStr]", "list[NewType]"], |
| 105 |
) |
| 106 |
def test_json_list_not_parsed_for_str_like_types(app, assert_parse_args, type_hint, wrapper): |
| 107 |
"""JSON arrays should NOT be parsed for list of string-like types.""" |
| 108 |
|
| 109 |
@app.default |
| 110 |
def main(values: type_hint): |
| 111 |
pass |
| 112 |
|
| 113 |
assert_parse_args( |
| 114 |
main, |
| 115 |
["--values", '["hello", "world"]'], |
| 116 |
[wrapper('["hello", "world"]')], |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize( |
| 126 |
"type_hint,json_input,expected", |
| 127 |
[ |
| 128 |
|
| 129 |
(list[str | int], "[1, 2, 3]", ["[1, 2, 3]"]), |
| 130 |
(list[str | int], '["hello", "world"]', ['["hello", "world"]']), |
| 131 |
|
| 132 |
(list[str | None], '["hello", null, "world"]', ['["hello", null, "world"]']), |
| 133 |
|
| 134 |
(list[int | None], "[1, null, 3]", [1, None, 3]), |
| 135 |
], |
| 136 |
ids=["Union[str,int]-ints", "Union[str,int]-strings", "Optional[str]", "Optional[int]"], |
| 137 |
) |
| 138 |
def test_json_list_parsing_with_unions(app, assert_parse_args, type_hint, json_input, expected): |
| 139 |
"""Test JSON array parsing behavior with Union types. |
| 140 |
|
| 141 |
When Union contains str, JSON parsing is disabled since the JSON string |
| 142 |
is a valid str value. |
| 143 |
""" |
| 144 |
|
| 145 |
@app.default |
| 146 |
def main(values: type_hint): |
| 147 |
pass |
| 148 |
|
| 149 |
assert_parse_args( |
| 150 |
main, |
| 151 |
["--values", json_input], |
| 152 |
expected, |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
def test_union_with_str_no_json_parsing(app, assert_parse_args): |
| 157 |
"""JSON should NOT be parsed when type is Union[str, ...].""" |
| 158 |
|
| 159 |
@app.default |
| 160 |
def main(value: str | int): |
| 161 |
pass |
| 162 |
|
| 163 |
assert_parse_args( |
| 164 |
main, |
| 165 |
["--value", '{"name": "Alice"}'], |
| 166 |
'{"name": "Alice"}', |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
def test_union_with_str_no_json_parsing_env_var(app, assert_parse_args, monkeypatch): |
| 171 |
"""JSON should NOT be parsed from env var when type is Union[str, ...].""" |
| 172 |
json_str = '{"name": "Alice"}' |
| 173 |
monkeypatch.setenv("VALUE", json_str) |
| 174 |
|
| 175 |
@app.default |
| 176 |
def main(value: Annotated[str | int, Parameter(env_var="VALUE")]): |
| 177 |
pass |
| 178 |
|
| 179 |
assert_parse_args(main, "", json_str) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@pytest.mark.parametrize( |
| 188 |
"dataclass_type,expected", |
| 189 |
[ |
| 190 |
(UserWithCustomStr, UserWithCustomStr(CustomStr("Alice"), 30)), |
| 191 |
(UserWithNewType, UserWithNewType(MyStr("Alice"), 30)), |
| 192 |
], |
| 193 |
ids=["CustomStr-field", "NewType-field"], |
| 194 |
) |
| 195 |
def test_json_dataclass_with_special_str_fields(app, assert_parse_args, dataclass_type, expected): |
| 196 |
"""JSON object parsing works for dataclasses with special string field types.""" |
| 197 |
|
| 198 |
@app.default |
| 199 |
def main(user: dataclass_type): |
| 200 |
pass |
| 201 |
|
| 202 |
assert_parse_args( |
| 203 |
main, |
| 204 |
["--user", '{"name": "Alice", "age": 30}'], |
| 205 |
expected, |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
def test_json_dataclass_with_union_field(app, assert_parse_args): |
| 210 |
"""JSON object parsing works for dataclasses with Union fields.""" |
| 211 |
|
| 212 |
@app.default |
| 213 |
def main(user: UserWithUnion): |
| 214 |
pass |
| 215 |
|
| 216 |
|
| 217 |
assert_parse_args( |
| 218 |
main, |
| 219 |
["--user", '{"name": "Alice", "age": 30}'], |
| 220 |
UserWithUnion("Alice", 30), |
| 221 |
) |
| 222 |
|
| 223 |
|
| 224 |
assert_parse_args( |
| 225 |
main, |
| 226 |
["--user", '{"name": 123, "age": 30}'], |
| 227 |
UserWithUnion("123", 30), |
| 228 |
) |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.parametrize( |
| 232 |
"dataclass_type,expected_type,name_wrapper", |
| 233 |
[ |
| 234 |
(UserWithCustomStr, UserWithCustomStr, CustomStr), |
| 235 |
(UserWithNewType, UserWithNewType, MyStr), |
| 236 |
], |
| 237 |
ids=["list[CustomStr-dataclass]", "list[NewType-dataclass]"], |
| 238 |
) |
| 239 |
def test_json_list_of_dataclass_with_special_fields( |
| 240 |
app, assert_parse_args, dataclass_type, expected_type, name_wrapper |
| 241 |
): |
| 242 |
"""JSON array parsing works for list of dataclasses with special string fields.""" |
| 243 |
|
| 244 |
@app.default |
| 245 |
def main(users: list[dataclass_type]): |
| 246 |
pass |
| 247 |
|
| 248 |
assert_parse_args( |
| 249 |
main, |
| 250 |
["--users", '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 40}]'], |
| 251 |
[expected_type(name_wrapper("Alice"), 30), expected_type(name_wrapper("Bob"), 40)], |
| 252 |
) |
| 253 |
|
| 254 |
|
| 255 |
@pytest.mark.parametrize( |
| 256 |
"type_hint,cmd_args,expected", |
| 257 |
[ |
| 258 |
|
| 259 |
( |
| 260 |
list[User], |
| 261 |
["--values", '{"name": "Alice", "age": 30}', "--values", '{"name": "Bob", "age": 40}'], |
| 262 |
[User("Alice", 30), User("Bob", 40)], |
| 263 |
), |
| 264 |
|
| 265 |
( |
| 266 |
list[CustomStr], |
| 267 |
["--values", '{"name": "Alice"}', "--values", '{"name": "Bob"}'], |
| 268 |
[CustomStr('{"name": "Alice"}'), CustomStr('{"name": "Bob"}')], |
| 269 |
), |
| 270 |
], |
| 271 |
ids=["list[Dataclass]-parses", "list[CustomStr]-no-parse"], |
| 272 |
) |
| 273 |
def test_multiple_json_objects(app, assert_parse_args, type_hint, cmd_args, expected): |
| 274 |
"""Test multiple individual JSON objects for lists.""" |
| 275 |
|
| 276 |
@app.default |
| 277 |
def main(values: type_hint): |
| 278 |
pass |
| 279 |
|
| 280 |
assert_parse_args(main, cmd_args, expected) |