MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / test_nested_discriminated_union

Function test_nested_discriminated_union

tests/test_models.py:989–1031  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

987
988
989def test_nested_discriminated_union() -> None:
990 class InnerType1(BaseModel):
991 type: Literal["type_1"]
992
993 class InnerModel(BaseModel):
994 inner_value: str
995
996 class InnerType2(BaseModel):
997 type: Literal["type_2"]
998 some_inner_model: InnerModel
999
1000 class Type1(BaseModel):
1001 base_type: Literal["base_type_1"]
1002 value: Annotated[
1003 Union[
1004 InnerType1,
1005 InnerType2,
1006 ],
1007 UnionDiscriminator("type"),
1008 ]
1009
1010 class Type2(BaseModel):
1011 base_type: Literal["base_type_2"]
1012
1013 T = Annotated[
1014 Union[
1015 Type1,
1016 Type2,
1017 ],
1018 UnionDiscriminator("base_type"),
1019 ]
1020
1021 model = construct_type(
1022 type_=T,
1023 value={
1024 "base_type": "base_type_1",
1025 "value": {
1026 "type": "type_2",
1027 },
1028 },
1029 )
1030 assert isinstance(model, Type1)
1031 assert isinstance(model.value, InnerType2)
1032
1033
1034@pytest.mark.skipif(PYDANTIC_V1, reason="this is only supported in pydantic v2 for now")

Callers

nothing calls this directly

Calls 2

UnionDiscriminatorClass · 0.90
construct_typeFunction · 0.90

Tested by

no test coverage detected