| 393 | |
| 394 | |
| 395 | def test_dict_of_union() -> None: |
| 396 | class SubModel1(BaseModel): |
| 397 | name: str |
| 398 | |
| 399 | class SubModel2(BaseModel): |
| 400 | foo: str |
| 401 | |
| 402 | class Model(BaseModel): |
| 403 | data: Dict[str, Union[SubModel1, SubModel2]] |
| 404 | |
| 405 | m = Model.construct(data={"hello": {"name": "there"}, "foo": {"foo": "bar"}}) |
| 406 | assert len(list(m.data.keys())) == 2 |
| 407 | assert isinstance(m.data["hello"], SubModel1) |
| 408 | assert m.data["hello"].name == "there" |
| 409 | assert isinstance(m.data["foo"], SubModel2) |
| 410 | assert m.data["foo"].foo == "bar" |
| 411 | |
| 412 | # TODO: test mismatched type |
| 413 | |
| 414 | |
| 415 | def test_double_nested_union() -> None: |