|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import Any, List, Union, Optional |
| 3 | +from typing import Any, List, Union, Iterable, Optional, cast |
4 | 4 | from datetime import date, datetime
|
5 | 5 | from typing_extensions import Required, Annotated, TypedDict
|
6 | 6 |
|
@@ -265,3 +265,35 @@ def test_pydantic_default_field() -> None:
|
265 | 265 | assert model.with_none_default == "bar"
|
266 | 266 | assert model.with_str_default == "baz"
|
267 | 267 | assert transform(model, Any) == {"with_none_default": "bar", "with_str_default": "baz"}
|
| 268 | + |
| 269 | + |
| 270 | +class TypedDictIterableUnion(TypedDict): |
| 271 | + foo: Annotated[Union[Bar8, Iterable[Baz8]], PropertyInfo(alias="FOO")] |
| 272 | + |
| 273 | + |
| 274 | +class Bar8(TypedDict): |
| 275 | + foo_bar: Annotated[str, PropertyInfo(alias="fooBar")] |
| 276 | + |
| 277 | + |
| 278 | +class Baz8(TypedDict): |
| 279 | + foo_baz: Annotated[str, PropertyInfo(alias="fooBaz")] |
| 280 | + |
| 281 | + |
| 282 | +def test_iterable_of_dictionaries() -> None: |
| 283 | + assert transform({"foo": [{"foo_baz": "bar"}]}, TypedDictIterableUnion) == {"FOO": [{"fooBaz": "bar"}]} |
| 284 | + assert cast(Any, transform({"foo": ({"foo_baz": "bar"},)}, TypedDictIterableUnion)) == {"FOO": [{"fooBaz": "bar"}]} |
| 285 | + |
| 286 | + def my_iter() -> Iterable[Baz8]: |
| 287 | + yield {"foo_baz": "hello"} |
| 288 | + yield {"foo_baz": "world"} |
| 289 | + |
| 290 | + assert transform({"foo": my_iter()}, TypedDictIterableUnion) == {"FOO": [{"fooBaz": "hello"}, {"fooBaz": "world"}]} |
| 291 | + |
| 292 | + |
| 293 | +class TypedDictIterableUnionStr(TypedDict): |
| 294 | + foo: Annotated[Union[str, Iterable[Baz8]], PropertyInfo(alias="FOO")] |
| 295 | + |
| 296 | + |
| 297 | +def test_iterable_union_str() -> None: |
| 298 | + assert transform({"foo": "bar"}, TypedDictIterableUnionStr) == {"FOO": "bar"} |
| 299 | + assert cast(Any, transform(iter([{"foo_baz": "bar"}]), Union[str, Iterable[Baz8]])) == [{"fooBaz": "bar"}] |
0 commit comments