| 374 | |
| 375 | |
| 376 | def test_dict_of_union() -> None: |
| 377 | class SubModel1(BaseModel): |
| 378 | name: str |
| 379 | |
| 380 | class SubModel2(BaseModel): |
| 381 | foo: str |
| 382 | |
| 383 | class Model(BaseModel): |
| 384 | data: Dict[str, Union[SubModel1, SubModel2]] |
| 385 | |
| 386 | m = Model.construct(data={"hello": {"name": "there"}, "foo": {"foo": "bar"}}) |
| 387 | assert len(list(m.data.keys())) == 2 |
| 388 | assert isinstance(m.data["hello"], SubModel1) |
| 389 | assert m.data["hello"].name == "there" |
| 390 | assert isinstance(m.data["foo"], SubModel2) |
| 391 | assert m.data["foo"].foo == "bar" |
| 392 | |
| 393 | # TODO: test mismatched type |
| 394 | |
| 395 | |
| 396 | def test_double_nested_union() -> None: |