MCPcopy
hub / github.com/pydantic/pydantic / test_from_attributes

Function test_from_attributes

tests/test_deprecated.py:129–162  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

127
128
129def test_from_attributes():
130 class PetCls:
131 def __init__(self, *, name: str, species: str):
132 self.name = name
133 self.species = species
134
135 class PersonCls:
136 def __init__(self, *, name: str, age: float = None, pets: list[PetCls]):
137 self.name = name
138 self.age = age
139 self.pets = pets
140
141 class Pet(BaseModel):
142 model_config = ConfigDict(from_attributes=True)
143 name: str
144 species: str
145
146 class Person(BaseModel):
147 model_config = ConfigDict(from_attributes=True)
148 name: str
149 age: float = None
150 pets: list[Pet]
151
152 bones = PetCls(name='Bones', species='dog')
153 orion = PetCls(name='Orion', species='cat')
154 anna = PersonCls(name='Anna', age=20, pets=[bones, orion])
155
156 anna_model = deprecated_from_orm(Person, anna)
157
158 assert anna_model.model_dump() == {
159 'name': 'Anna',
160 'pets': [{'name': 'Bones', 'species': 'dog'}, {'name': 'Orion', 'species': 'cat'}],
161 'age': 20.0,
162 }
163
164
165def test_not_from_attributes():

Callers

nothing calls this directly

Calls 4

PetClsClass · 0.85
PersonClsClass · 0.85
deprecated_from_ormFunction · 0.85
model_dumpMethod · 0.45

Tested by

no test coverage detected