Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Field.include #6852

Merged
merged 2 commits into from Jul 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 @@ -636,7 +631,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 @@ -671,7 +665,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 @@ -713,7 +706,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 @@ -801,6 +793,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, this is not a deprecation... If the functionality is removed, it's a removal. The parameter itself is deprecated.

Also, how is exclude a replacement for include? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, this is not a deprecation... If the functionality is removed, it's a removal. The parameter itself is deprecated.

I think it is a deprecation as it did nothing before and still does nothing. So, no change in functionality. just warning the user.

Also, how is exclude a replacement for include? 🤔

include=False can be replaced with exclude=True. That's why I suggested using exclude.


return FieldInfo.from_field(
default,
default_factory=default_factory,
Expand All @@ -812,7 +808,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