()
| 1006 | |
| 1007 | |
| 1008 | def test_nested_discriminated_union() -> None: |
| 1009 | class InnerType1(BaseModel): |
| 1010 | type: Literal["type_1"] |
| 1011 | |
| 1012 | class InnerModel(BaseModel): |
| 1013 | inner_value: str |
| 1014 | |
| 1015 | class InnerType2(BaseModel): |
| 1016 | type: Literal["type_2"] |
| 1017 | some_inner_model: InnerModel |
| 1018 | |
| 1019 | class Type1(BaseModel): |
| 1020 | base_type: Literal["base_type_1"] |
| 1021 | value: Annotated[ |
| 1022 | Union[ |
| 1023 | InnerType1, |
| 1024 | InnerType2, |
| 1025 | ], |
| 1026 | UnionDiscriminator("type"), |
| 1027 | ] |
| 1028 | |
| 1029 | class Type2(BaseModel): |
| 1030 | base_type: Literal["base_type_2"] |
| 1031 | |
| 1032 | T = Annotated[ |
| 1033 | Union[ |
| 1034 | Type1, |
| 1035 | Type2, |
| 1036 | ], |
| 1037 | UnionDiscriminator("base_type"), |
| 1038 | ] |
| 1039 | |
| 1040 | model = construct_type( |
| 1041 | type_=T, |
| 1042 | value={ |
| 1043 | "base_type": "base_type_1", |
| 1044 | "value": { |
| 1045 | "type": "type_2", |
| 1046 | }, |
| 1047 | }, |
| 1048 | ) |
| 1049 | assert isinstance(model, Type1) |
| 1050 | assert isinstance(model.value, InnerType2) |
| 1051 | |
| 1052 | |
| 1053 | @pytest.mark.skipif(PYDANTIC_V1, reason="this is only supported in pydantic v2 for now") |
nothing calls this directly
no test coverage detected