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

Add support for dataclass fields init #8552

Merged
merged 9 commits into from Jan 19, 2024
Merged
1 change: 1 addition & 0 deletions docs/concepts/fields.md
Expand Up @@ -459,6 +459,7 @@ print(foo)

There are fields that can be used to constrain dataclasses:

* `init`: Whether the field should be included in the `__init__` of the dataclass.
* `init_var`: Whether the field should be seen as an [init-only field] in the dataclass.
* `kw_only`: Whether the field should be a keyword-only argument in the constructor of the dataclass.

Expand Down
1 change: 1 addition & 0 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -941,6 +941,7 @@ def _generate_dc_field_schema(
return core_schema.dataclass_field(
name,
common_field['schema'],
init=field_info.init,
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
init_only=field_info.init_var or None,
kw_only=None if field_info.kw_only else False,
serialization_exclude=common_field['serialization_exclude'],
Expand Down
15 changes: 13 additions & 2 deletions pydantic/fields.py
Expand Up @@ -64,6 +64,7 @@ class _FromFieldInfoInputs(typing_extensions.TypedDict, total=False):
frozen: bool | None
validate_default: bool | None
repr: bool
init: bool | None
init_var: bool | None
kw_only: bool | None

Expand Down Expand Up @@ -101,7 +102,8 @@ class FieldInfo(_repr.Representation):
frozen: Whether the field is frozen.
validate_default: Whether to validate the default value of the field.
repr: Whether to include the field in representation of the model.
init_var: Whether the field should be included in the constructor of the dataclass.
init: Whether the field should be included in the constructor of the dataclass.
init_var: Whether the field should _only_ be included in the constructor of the dataclass, and not stored.
kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
metadata: List of metadata constraints.
"""
Expand All @@ -122,6 +124,7 @@ class FieldInfo(_repr.Representation):
frozen: bool | None
validate_default: bool | None
repr: bool
init: bool | None
init_var: bool | None
kw_only: bool | None
metadata: list[Any]
Expand All @@ -143,6 +146,7 @@ class FieldInfo(_repr.Representation):
'frozen',
'validate_default',
'repr',
'init',
'init_var',
'kw_only',
'metadata',
Expand Down Expand Up @@ -203,6 +207,7 @@ def __init__(self, **kwargs: Unpack[_FieldInfoInputs]) -> None:
self.validate_default = kwargs.pop('validate_default', None)
self.frozen = kwargs.pop('frozen', None)
# currently only used on dataclasses
self.init = kwargs.pop('init', None)
self.init_var = kwargs.pop('init_var', None)
self.kw_only = kwargs.pop('kw_only', None)

Expand Down Expand Up @@ -360,6 +365,7 @@ class MyModel(pydantic.BaseModel):
)
pydantic_field.frozen = final or pydantic_field.frozen
pydantic_field.init_var = init_var
pydantic_field.init = getattr(default, 'init', None)
pydantic_field.kw_only = getattr(default, 'kw_only', None)
return pydantic_field
else:
Expand Down Expand Up @@ -596,6 +602,7 @@ class _EmptyKwargs(typing_extensions.TypedDict):
frozen=None,
validate_default=None,
repr=True,
init=None,
init_var=None,
kw_only=None,
pattern=None,
Expand Down Expand Up @@ -630,6 +637,7 @@ def Field( # noqa: C901
frozen: bool | None = _Unset,
validate_default: bool | None = _Unset,
repr: bool = _Unset,
init: bool | None = _Unset,
init_var: bool | None = _Unset,
kw_only: bool | None = _Unset,
pattern: str | None = _Unset,
Expand Down Expand Up @@ -675,7 +683,9 @@ def Field( # noqa: C901
validate_default: If `True`, apply validation to the default value every time you create an instance.
Otherwise, for performance reasons, the default value of the field is trusted and not validated.
repr: A boolean indicating whether to include the field in the `__repr__` output.
init_var: Whether the field should be included in the constructor of the dataclass.
init: Whether the field should be included in the constructor of the dataclass.
(Only applies to dataclasses.)
init_var: Whether the field should _only_ be included in the constructor of the dataclass.
(Only applies to dataclasses.)
kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
(Only applies to dataclasses.)
Expand Down Expand Up @@ -784,6 +794,7 @@ def Field( # noqa: C901
pattern=pattern,
validate_default=validate_default,
repr=repr,
init=init,
init_var=init_var,
kw_only=kw_only,
strict=strict,
Expand Down