MCPcopy
hub / github.com/pydantic/pydantic / test_serialize_extra_allow_subclass_2

Function test_serialize_extra_allow_subclass_2

tests/test_serialize.py:61–80  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

59
60
61def test_serialize_extra_allow_subclass_2() -> None:
62 class Parent(BaseModel):
63 x: int
64 model_config = ConfigDict(extra='allow')
65
66 class Child(Parent):
67 y: int
68
69 class Model(BaseModel):
70 inner: Parent
71
72 m = Model(inner=Child(x=1, y=2))
73 assert m.inner.y == 2
74 assert m.model_dump() == {'inner': {'x': 1}}
75 assert json.loads(m.model_dump_json()) == {'inner': {'x': 1}}
76
77 m = Model(inner=Parent(x=1, y=2))
78 assert m.inner.y == 2
79 assert m.model_dump() == {'inner': {'x': 1, 'y': 2}}
80 assert json.loads(m.model_dump_json()) == {'inner': {'x': 1, 'y': 2}}
81
82
83def test_serializer_annotated_plain_always():

Callers

nothing calls this directly

Calls 5

model_dump_jsonMethod · 0.80
ModelClass · 0.70
ChildClass · 0.70
ParentClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected