MCPcopy
hub / github.com/pydantic/pydantic / test_include_exclude

Function test_include_exclude

tests/test_computed_fields.py:287–304  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

285
286
287def test_include_exclude():
288 class Model(BaseModel):
289 x: int
290 y: int
291
292 @computed_field
293 def x_list(self) -> list[int]:
294 return [self.x, self.x + 1]
295
296 @computed_field
297 def y_list(self) -> list[int]:
298 return [self.y, self.y + 1, self.y + 2]
299
300 m = Model(x=1, y=2)
301 assert m.model_dump() == {'x': 1, 'y': 2, 'x_list': [1, 2], 'y_list': [2, 3, 4]}
302 assert m.model_dump(include={'x'}) == {'x': 1}
303 assert m.model_dump(include={'x': None, 'x_list': {0}}) == {'x': 1, 'x_list': [1]}
304 assert m.model_dump(exclude={'x': ..., 'y_list': {2}}) == {'y': 2, 'x_list': [1, 2], 'y_list': [2, 3]}
305
306
307def test_exclude_none():

Callers

nothing calls this directly

Calls 2

ModelClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected