MCPcopy
hub / github.com/pydantic/pydantic / test_slots_are_ignored

Function test_slots_are_ignored

tests/test_private_attributes.py:217–243  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

215
216
217def test_slots_are_ignored():
218 class Model(BaseModel):
219 __slots__ = (
220 'foo',
221 '_bar',
222 )
223
224 def __init__(self):
225 super().__init__()
226 for attr_ in self.__slots__:
227 object.__setattr__(self, attr_, 'spam')
228
229 assert Model.__private_attributes__ == {}
230 assert set(Model.__slots__) == {'foo', '_bar'}
231 m1 = Model()
232 m2 = Model()
233
234 for attr in Model.__slots__:
235 assert object.__getattribute__(m1, attr) == 'spam'
236
237 # In v2, you are always allowed to set instance attributes if the name starts with `_`.
238 m1._bar = 'not spam'
239 assert m1._bar == 'not spam'
240 assert m2._bar == 'spam'
241
242 with pytest.raises(ValueError, match='"Model" object has no field "foo"'):
243 m1.foo = 'not spam'
244
245
246def test_default_and_default_factory_used_error():

Callers

nothing calls this directly

Calls 2

ModelClass · 0.70
__getattribute__Method · 0.45

Tested by

no test coverage detected