Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pydantic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def model_dump(
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
fallback: Callable[[Any], Any] | None = None,
Expand All @@ -447,6 +448,9 @@ def model_dump(
exclude_unset: Whether to exclude fields that have not been explicitly set.
exclude_defaults: Whether to exclude fields that are set to their default value.
exclude_none: Whether to exclude fields that have a value of `None`.
exclude_computed_fields: Whether to exclude computed fields.
While this can be useful for round-tripping, it is usually recommended tu use the dedicated
`round_trip` parameter instead.
round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
Expand All @@ -467,6 +471,7 @@ def model_dump(
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
exclude_computed_fields=exclude_computed_fields,
round_trip=round_trip,
warnings=warnings,
fallback=fallback,
Expand All @@ -485,6 +490,7 @@ def model_dump_json(
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
fallback: Callable[[Any], Any] | None = None,
Expand All @@ -506,6 +512,9 @@ def model_dump_json(
exclude_unset: Whether to exclude fields that have not been explicitly set.
exclude_defaults: Whether to exclude fields that are set to their default value.
exclude_none: Whether to exclude fields that have a value of `None`.
exclude_computed_fields: Whether to exclude computed fields.
While this can be useful for round-tripping, it is usually recommended to use the dedicated
`round_trip` parameter instead.
round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
Expand All @@ -527,6 +536,7 @@ def model_dump_json(
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
exclude_computed_fields=exclude_computed_fields,
round_trip=round_trip,
warnings=warnings,
fallback=fallback,
Expand Down
1 change: 1 addition & 0 deletions pydantic/root_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def model_dump( # type: ignore
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
serialize_as_any: bool = False,
Expand Down
10 changes: 10 additions & 0 deletions pydantic/type_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ def dump_python(
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
fallback: Callable[[Any], Any] | None = None,
Expand All @@ -587,6 +588,9 @@ def dump_python(
exclude_unset: Whether to exclude unset fields.
exclude_defaults: Whether to exclude fields with default values.
exclude_none: Whether to exclude fields with None values.
exclude_computed_fields: Whether to exclude computed fields.
While this can be useful for round-tripping, it is usually recommended to use the dedicated
`round_trip` parameter instead.
round_trip: Whether to output the serialized data in a way that is compatible with deserialization.
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
Expand All @@ -607,6 +611,7 @@ def dump_python(
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
exclude_computed_fields=exclude_computed_fields,
round_trip=round_trip,
warnings=warnings,
fallback=fallback,
Expand All @@ -627,6 +632,7 @@ def dump_json(
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
fallback: Callable[[Any], Any] | None = None,
Expand All @@ -649,6 +655,9 @@ def dump_json(
exclude_unset: Whether to exclude unset fields.
exclude_defaults: Whether to exclude fields with default values.
exclude_none: Whether to exclude fields with a value of `None`.
exclude_computed_fields: Whether to exclude computed fields.
While this can be useful for round-tripping, it is usually recommended to use the dedicated
`round_trip` parameter instead.
round_trip: Whether to serialize and deserialize the instance to ensure round-tripping.
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
Expand All @@ -670,6 +679,7 @@ def dump_json(
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
exclude_computed_fields=exclude_computed_fields,
round_trip=round_trip,
warnings=warnings,
fallback=fallback,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_computed_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,18 @@ def my_field_serializer(self, value: Any, info: FieldSerializationInfo) -> Any:
return f'{info.field_name} = {value}'

assert MyModel().model_dump() == {'my_field': 'my_field = foo', 'other_field': 'other_field = 42'}


def test_computed_fields_exclude() -> None:
class Model(BaseModel):
@computed_field
def prop(self) -> int:
return 1

m = Model()
assert m.model_dump() == {'prop': 1}
assert m.model_dump(exclude_computed_fields=True) == {}
assert m.model_dump(mode='json') == {'prop': 1}
assert m.model_dump(mode='json', exclude_computed_fields=True) == {}
assert m.model_dump_json() == '{"prop":1}'
assert m.model_dump_json(exclude_computed_fields=True) == '{}'
Loading