pydantic.dataclasses
Provide an enhanced dataclass that performs validation.
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.
config : ConfigDict | type[object] | None Default: None
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.