Skip to content

Commit

Permalink
Deprecate Field.include
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Jul 25, 2023
1 parent f8c081e commit 42a0f88
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/usage/fields.md
Expand Up @@ -652,9 +652,9 @@ except ValidationError as e:

2. Since `validate_assignment` is enabled, and the `name` field is frozen, the assignment is not allowed.

## Include and Exclude
## Exclude

The parameters `include` and `exclude` can be used to control which fields should be included or excluded from the
The `exclude` parameter can be used to control which fields should be excluded from the
model when exporting the model.

See the following example:
Expand Down
8 changes: 4 additions & 4 deletions docs/usage/serialization.md
Expand Up @@ -634,7 +634,7 @@ The same holds for the `model_dump_json` method.
### Model- and field-level include and exclude

In addition to the explicit arguments `exclude` and `include` passed to `model_dump` and `model_dump_json` methods,
we can also pass the `include`/`exclude` arguments directly to the `Field` constructor:
we can also pass the `exclude` arguments directly to the `Field` constructor:

Setting `exclude` on the field constructor (`Field(..., exclude=True)`) takes priority over the
`exclude`/`include` on `model_dump` and `model_dump_json`:
Expand Down Expand Up @@ -705,12 +705,12 @@ print(t.model_dump(exclude={'value': True, 'user': {'username'}}))
are the same as using merged include settings as follows:

```py
from pydantic import BaseModel, Field, SecretStr
from pydantic import BaseModel, SecretStr


class User(BaseModel):
id: int = Field(..., include=True)
username: str = Field(..., include=True) # overridden by explicit include
id: int
username: str # overridden by explicit include
password: SecretStr


Expand Down
2 changes: 1 addition & 1 deletion pydantic/deprecated/copy_internals.py
Expand Up @@ -44,7 +44,7 @@ def _iter(
)

if include is not None:
include = _utils.ValueItems.merge({k: v.include for k, v in self.model_fields.items()}, include, intersect=True)
include = _utils.ValueItems.merge({k: True for k in self.model_fields}, include, intersect=True)

allowed_keys = _calculate_keys(self, include=include, exclude=exclude, exclude_unset=exclude_unset) # type: ignore
if allowed_keys is None and not (to_dict or by_alias or exclude_unset or exclude_defaults or exclude_none):
Expand Down
13 changes: 4 additions & 9 deletions pydantic/fields.py
Expand Up @@ -44,7 +44,6 @@ class _FromFieldInfoInputs(typing_extensions.TypedDict, total=False):
description: str | None
examples: list[Any] | None
exclude: bool | None
include: bool | None
gt: float | None
ge: float | None
lt: float | None
Expand Down Expand Up @@ -94,7 +93,6 @@ class FieldInfo(_repr.Representation):
description: The description of the field.
examples: List of examples of the field.
exclude: Whether to exclude the field from the model schema.
include: Whether to include the field in the model schema.
discriminator: Field name for discriminating the type in a tagged union.
json_schema_extra: Dictionary of extra JSON schema properties.
frozen: Whether the field is frozen.
Expand All @@ -116,7 +114,6 @@ class FieldInfo(_repr.Representation):
description: str | None
examples: list[Any] | None
exclude: bool | None
include: bool | None
discriminator: str | None
json_schema_extra: dict[str, Any] | typing.Callable[[dict[str, Any]], None] | None
frozen: bool | None
Expand All @@ -138,7 +135,6 @@ class FieldInfo(_repr.Representation):
'description',
'examples',
'exclude',
'include',
'discriminator',
'json_schema_extra',
'frozen',
Expand Down Expand Up @@ -197,7 +193,6 @@ def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None:
self.description = kwargs.pop('description', None)
self.examples = kwargs.pop('examples', None)
self.exclude = kwargs.pop('exclude', None)
self.include = kwargs.pop('include', None)
self.discriminator = kwargs.pop('discriminator', None)
self.repr = kwargs.pop('repr', True)
self.json_schema_extra = kwargs.pop('json_schema_extra', None)
Expand Down Expand Up @@ -628,7 +623,6 @@ class _EmptyKwargs(typing_extensions.TypedDict):
description=None,
examples=None,
exclude=None,
include=None,
discriminator=None,
json_schema_extra=None,
frozen=None,
Expand Down Expand Up @@ -663,7 +657,6 @@ def Field( # noqa: C901
description: str | None = _Unset,
examples: list[Any] | None = _Unset,
exclude: bool | None = _Unset,
include: bool | None = _Unset,
discriminator: str | None = _Unset,
json_schema_extra: dict[str, Any] | typing.Callable[[dict[str, Any]], None] | None = _Unset,
frozen: bool | None = _Unset,
Expand Down Expand Up @@ -705,7 +698,6 @@ def Field( # noqa: C901
description: Human-readable description.
examples: Example values for this field.
exclude: Whether to exclude the field from the model schema.
include: Whether to include the field in the model schema.
discriminator: Field name for discriminating the type in a tagged union.
json_schema_extra: Any additional JSON schema data for the schema property.
frozen: Whether the field is frozen.
Expand Down Expand Up @@ -793,6 +785,10 @@ def Field( # noqa: C901
if validation_alias in (_Unset, None):
validation_alias = alias

include = extra.pop('include', None) # type: ignore
if include is not None:
warn('`include` is deprecated and does nothing. It will be removed, use `exclude` instead', DeprecationWarning)

return FieldInfo.from_field(
default,
default_factory=default_factory,
Expand All @@ -804,7 +800,6 @@ def Field( # noqa: C901
description=description,
examples=examples,
exclude=exclude,
include=include,
discriminator=discriminator,
json_schema_extra=json_schema_extra,
frozen=frozen,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_deprecated.py
Expand Up @@ -431,6 +431,14 @@ class Model(BaseModel):
x: str = Field('test', const=True)


def test_field_include_deprecation():
m = '`include` is deprecated and does nothing. It will be removed, use `exclude` instead'
with pytest.warns(PydanticDeprecatedSince20, match=m):

class Model(BaseModel):
x: int = Field(include=True)


def test_unique_items_items():
with pytest.raises(PydanticUserError, match='`unique_items` is removed. use `Set` instead'):

Expand Down

0 comments on commit 42a0f88

Please sign in to comment.