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

Function test_from_attributes

tests/test_deprecated.py:129–162  ·  tests/test_deprecated.py::test_from_attributes
()

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=&class="cm">#x27;Bonesclass="st">', species='dog')
153 orion = PetCls(name=&class="cm">#x27;Orionclass="st">', species='cat')
154 anna = PersonCls(name=&class="cm">#x27;Anna', age=20, pets=[bones, orion])
155
156 anna_model = deprecated_from_orm(Person, anna)
157
158 assert anna_model.model_dump() == {
159 &class="cm">#x27;nameclass="st">': 'Anna',
160 &class="cm">#x27;petsclass="st">': [{'nameclass="st">': 'Bonesclass="st">', 'speciesclass="st">': 'dogclass="st">'}, {'nameclass="st">': 'Orionclass="st">', 'speciesclass="st">': 'cat'}],
161 &class="cm">#x27;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