MCPcopy
hub / github.com/pydantic/pydantic / test_private_attribute_factory

Function test_private_attribute_factory

tests/test_private_attributes.py:68–88  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

66
67
68def test_private_attribute_factory():
69 default = {'a': {}}
70
71 def factory() -> dict[str, dict]:
72 return default
73
74 class Model(BaseModel):
75 _foo = PrivateAttr(default_factory=factory)
76
77 assert Model.__private_attributes__ == {'_foo': PrivateAttr(default_factory=factory)}
78
79 m = Model()
80 assert m._foo == default
81 assert m._foo is default
82 assert m._foo['a'] is default['a']
83
84 m._foo = None
85 assert m._foo is None
86
87 assert m.model_dump() == {}
88 assert m.__dict__ == {}
89
90
91def test_private_attribute_factory_uses_validated_data():

Callers

nothing calls this directly

Calls 3

PrivateAttrFunction · 0.90
ModelClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected