MCPcopy
hub / github.com/pydantic/pydantic / test_dataclass_field_exclude

Function test_dataclass_field_exclude

tests/test_dataclasses.py:3270–3284  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3268
3269
3270def test_dataclass_field_exclude() -> None:
3271 @pydantic.dataclasses.dataclass
3272 class Foo:
3273 foo: str = Field(exclude=True)
3274 bar: int = Field(exclude_if=lambda x: x > 1)
3275
3276 ta = TypeAdapter(Foo)
3277
3278 assert ta.dump_python(Foo(foo='bar', bar=1)) == {'bar': 1}
3279 assert ta.dump_python(Foo(foo='bar', bar=1), exclude={'bar'}) == {}
3280 assert ta.dump_python(Foo(foo='bar', bar=2)) == {}
3281
3282 assert ta.dump_json(Foo(foo='bar', bar=1)).decode('utf-8') == '{"bar":1}'
3283 assert ta.dump_json(Foo(foo='bar', bar=1), exclude={'bar'}).decode('utf-8') == '{}'
3284 assert ta.dump_json(Foo(foo='bar', bar=2)).decode('utf-8') == '{}'
3285
3286
3287@pytest.mark.skipif(sys.version_info < (3, 10), reason='kw_only is not available in python >= 3.10')

Callers

nothing calls this directly

Calls 5

dump_pythonMethod · 0.95
dump_jsonMethod · 0.95
TypeAdapterClass · 0.90
FooClass · 0.70
decodeMethod · 0.45

Tested by

no test coverage detected