MCPcopy Create free account
hub / github.com/anomalyco/opencode-sdk-python / test_nested_discriminated_union

Function test_nested_discriminated_union

tests/test_models.py:894–936  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

892
893
894def test_nested_discriminated_union() -> None:
895 class InnerType1(BaseModel):
896 type: Literal["type_1"]
897
898 class InnerModel(BaseModel):
899 inner_value: str
900
901 class InnerType2(BaseModel):
902 type: Literal["type_2"]
903 some_inner_model: InnerModel
904
905 class Type1(BaseModel):
906 base_type: Literal["base_type_1"]
907 value: Annotated[
908 Union[
909 InnerType1,
910 InnerType2,
911 ],
912 PropertyInfo(discriminator="type"),
913 ]
914
915 class Type2(BaseModel):
916 base_type: Literal["base_type_2"]
917
918 T = Annotated[
919 Union[
920 Type1,
921 Type2,
922 ],
923 PropertyInfo(discriminator="base_type"),
924 ]
925
926 model = construct_type(
927 type_=T,
928 value={
929 "base_type": "base_type_1",
930 "value": {
931 "type": "type_2",
932 },
933 },
934 )
935 assert isinstance(model, Type1)
936 assert isinstance(model.value, InnerType2)
937
938
939@pytest.mark.skipif(not PYDANTIC_V2, reason="this is only supported in pydantic v2 for now")

Callers

nothing calls this directly

Calls 2

PropertyInfoClass · 0.90
construct_typeFunction · 0.90

Tested by

no test coverage detected