MCPcopy
hub / github.com/pydantic/pydantic / test_exclude_none

Function test_exclude_none

tests/test_computed_fields.py:307–324  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

305
306
307def test_exclude_none():
308 class Model(BaseModel):
309 x: int
310 y: int
311
312 @computed_field
313 def sum(self) -> int:
314 return self.x + self.y
315
316 @computed_field
317 def none(self) -> None:
318 return None
319
320 m = Model(x=1, y=2)
321 assert m.model_dump(exclude_none=False) == {'x': 1, 'y': 2, 'sum': 3, 'none': None}
322 assert m.model_dump(exclude_none=True) == {'x': 1, 'y': 2, 'sum': 3}
323 assert m.model_dump(mode='json', exclude_none=False) == {'x': 1, 'y': 2, 'sum': 3, 'none': None}
324 assert m.model_dump(mode='json', exclude_none=True) == {'x': 1, 'y': 2, 'sum': 3}
325
326
327def test_expected_type():

Callers

nothing calls this directly

Calls 2

ModelClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected