MCPcopy
hub / github.com/pydantic/pydantic / test_private_attribute_annotation

Function test_private_attribute_annotation

tests/test_private_attributes.py:133–161  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

131
132
133def test_private_attribute_annotation():
134 class Model(BaseModel):
135 """The best model"""
136
137 _foo: str
138
139 assert Model.__private_attributes__ == {'_foo': PrivateAttr(PydanticUndefined)}
140 assert repr(Model.__doc__) == "'The best model'"
141
142 m = Model()
143 with pytest.raises(AttributeError):
144 m._foo
145
146 m._foo = '123'
147 assert m._foo == '123'
148
149 m._foo = None
150 assert m._foo is None
151
152 del m._foo
153
154 with pytest.raises(AttributeError):
155 m._foo
156
157 m._foo = '123'
158 assert m._foo == '123'
159
160 assert m.model_dump() == {}
161 assert m.__dict__ == {}
162
163
164def test_underscore_attrs_are_private():

Callers

nothing calls this directly

Calls 3

PrivateAttrFunction · 0.90
ModelClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected