MCPcopy
hub / github.com/pydantic/pydantic / test_object_with_getattr

Function test_object_with_getattr

tests/test_deprecated.py:174–198  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

172
173
174def test_object_with_getattr():
175 class FooGetAttr:
176 def __getattr__(self, key: str):
177 if key == 'foo':
178 return 'Foo'
179 else:
180 raise AttributeError
181
182 class Model(BaseModel):
183 model_config = ConfigDict(from_attributes=True)
184 foo: str
185 bar: int = 1
186
187 class ModelInvalid(BaseModel):
188 model_config = ConfigDict(from_attributes=True)
189 foo: str
190 bar: int
191
192 foo = FooGetAttr()
193 model = deprecated_from_orm(Model, foo)
194 assert model.foo == 'Foo'
195 assert model.bar == 1
196 assert model.model_dump(exclude_unset=True) == {'foo': 'Foo'}
197 with pytest.raises(ValidationError):
198 deprecated_from_orm(ModelInvalid, foo)
199
200
201def test_properties():

Callers

nothing calls this directly

Calls 3

FooGetAttrClass · 0.85
deprecated_from_ormFunction · 0.85
model_dumpMethod · 0.45

Tested by

no test coverage detected