pydantic.dataclasses
Provide an enhanced dataclass that performs validation.
Bases: TypedDict
A dictionary-like class for configuring Pydantic models.
Type: str | None
Type: bool
Type: bool
Type: bool
Type: int
Type: int | None
Type: ExtraValues | None
Type: bool
Type: bool
Type: bool
Type: bool
Type: bool
Type: bool
Type: bool
Type: Callable[[str], str] | None
Type: tuple[type, ...]
Type: bool
Type: dict[str, object] | JsonSchemaExtraCallable | None
Type: bool
Type: Literal['always', 'never', 'subclass-instances']
Type: Literal['iso8601', 'float']
Type: Literal['utf8', 'base64']
Type: bool
Type: bool
Type: tuple[str, ...]
Type: bool
Bases: StandardDataclass, Protocol
A protocol containing attributes only available once a class has been decorated as a Pydantic dataclass.
def getattr_migration(module: str) -> Callable[[str], Any]
Implement PEP 562 for objects that were either moved or removed on the migration to V2.
Callable[[str], Any] — A callable that will raise an error if the object is not found.
The module name.
def Field(
default: Any = PydanticUndefined,
default_factory: typing.Callable[[], Any] | None = _Unset,
alias: str | None = _Unset,
alias_priority: int | None = _Unset,
validation_alias: str | AliasPath | AliasChoices | None = _Unset,
serialization_alias: str | None = _Unset,
title: str | None = _Unset,
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] | None = _Unset,
frozen: bool | None = _Unset,
validate_default: bool | None = _Unset,
repr: bool = _Unset,
init_var: bool | None = _Unset,
kw_only: bool | None = _Unset,
pattern: str | None = _Unset,
strict: bool | None = _Unset,
gt: float | None = _Unset,
ge: float | None = _Unset,
lt: float | None = _Unset,
le: float | None = _Unset,
multiple_of: float | None = _Unset,
allow_inf_nan: bool | None = _Unset,
max_digits: int | None = _Unset,
decimal_places: int | None = _Unset,
min_length: int | None = _Unset,
max_length: int | None = _Unset,
extra: Unpack[_EmptyKwargs] = {},
) -> Any
Create a field for objects that can be configured.
Used to provide extra information about a field, either for the model schema or complex validation. Some arguments
apply only to number fields (int, float, Decimal) and some apply only to str.
Any — The generated FieldInfo object
Default value if the field is not set.
A callable to generate the default value, such as :func:~datetime.utcnow.
An alternative name for the attribute.
Priority of the alias. This affects whether an alias generator is used.
‘Whitelist’ validation step. The field will be the single one allowed by the alias or set of aliases defined.
‘Blacklist’ validation step. The vanilla field will be the single one of the alias’ or set of aliases’ fields and all the other fields will be ignored at serialization time.
Human-readable title.
Human-readable description.
Example values for this field.
Whether to exclude the field from the model schema.
Whether to include the field in the model schema.
Field name for discriminating the type in a tagged union.
Any additional JSON schema data for the schema property.
Whether the field is frozen.
Run validation that isn’t only checking existence of defaults. True by default.
A boolean indicating whether to include the field in the __repr__ output.
Whether the field should be included in the constructor of the dataclass.
Whether the field should be a keyword-only argument in the constructor of the dataclass.
If True, strict validation is applied to the field.
See Strict Mode for details.
Greater than. If set, value must be greater than this. Only applicable to numbers.
Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers.
Less than. If set, value must be less than this. Only applicable to numbers.
Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers.
Value must be a multiple of this. Only applicable to numbers.
Minimum length for strings.
Maximum length for strings.
Pattern for strings.
Allow inf, -inf, nan. Only applicable to numbers.
Maximum number of allow digits for strings.
Maximum number of decimal places allowed for numbers.
Include extra fields used by the JSON schema.
def dataclass(
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: ConfigDict | type[object] | None = None,
validate_on_init: bool | None = None,
kw_only: bool = ...,
slots: bool = ...,
) -> Callable[[type[_T]], type[PydanticDataclass]]
def dataclass(
_cls: type[_T],
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: ConfigDict | type[object] | None = None,
validate_on_init: bool | None = None,
kw_only: bool = ...,
slots: bool = ...,
) -> type[PydanticDataclass]
def dataclass(
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: ConfigDict | type[object] | None = None,
validate_on_init: bool | None = None,
) -> Callable[[type[_T]], type[PydanticDataclass]]
def dataclass(
_cls: type[_T],
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: ConfigDict | type[object] | None = None,
validate_on_init: bool | None = None,
) -> type[PydanticDataclass]
A decorator used to create a Pydantic-enhanced dataclass, similar to the standard Python dataclass,
but with added validation.
This function should be used similarly to dataclasses.dataclass.
Callable[[type[_T]], type[PydanticDataclass]] | type[PydanticDataclass] — A decorator that accepts a class as its argument and returns a Pydantic dataclass.
The target dataclass.
Included for signature compatibility with dataclasses.dataclass, and is passed through to
dataclasses.dataclass when appropriate. If specified, must be set to False, as pydantic inserts its
own __init__ function.
A boolean indicating whether or not to include the field in the __repr__ output.
Determines if a __eq__ should be generated for the class.
Determines if comparison magic methods should be generated, such as __lt__, but not __eq__.
Determines if an unsafe hashing function should be included in the class.
Determines if the generated class should be a ‘frozen’ dataclass, which does not allow its
attributes to be modified from its constructor.
A configuration for the dataclass generation.
A deprecated parameter included for backwards compatibility; in V2, all Pydantic dataclasses are validated on init.
Determines if __init__ method parameters must be specified by keyword only. Defaults to False.
Determines if the generated class should be a ‘slots’ dataclass, which does not allow the addition of
new attributes after instantiation.
AssertionError— Raised ifinitis notFalseorvalidate_on_initisFalse.
def rebuild_dataclass(
cls: type[PydanticDataclass],
force: bool = False,
raise_errors: bool = True,
_parent_namespace_depth: int = 2,
_types_namespace: dict[str, Any] | None = None,
) -> bool | None
Try to rebuild the pydantic-core schema for the dataclass.
This may be necessary when one of the annotations is a ForwardRef which could not be resolved during the initial attempt to build the schema, and automatic rebuilding fails.
This is analogous to BaseModel.model_rebuild.
bool | None — Returns None if the schema is already “complete” and rebuilding was not required.
bool | None — If rebuilding was required, returns True if rebuilding was successful, otherwise False.
The class to build the dataclass core schema for.
Whether to force the rebuilding of the model schema, defaults to False.
Whether to raise errors, defaults to True.
The depth level of the parent namespace, defaults to 2.
The types namespace, defaults to None.