Files
pydantic/docs/usage/dataclasses.md
T
Koudai Aono 33b3dc1825 mypy plugin support for dataclasses (#966)
* mypy plugin support for dataclassesv

* fix styles and types

* - change type-hint for `Config`
- change name of an expected file
- update documents

* fix broken a reference of a document.

* - update unittest
- update documents

* fix a document link

* Update docs/mypy_plugin.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update docs/mypy_plugin.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update docs/mypy_plugin.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* remove extra whitespaces on mypy test results

* fix output file name of mypy test

* Update docs/usage/dataclasses.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* use TypeVar for DataclassType
2019-11-14 11:26:46 +00:00

2.5 KiB

If you don't want to use pydantic's BaseModel you can instead get the same data validation on standard dataclasses (introduced in python 3.7).

Dataclasses work in python 3.6 using the dataclasses backport package.

{!.tmp_examples/dataclasses_main.py!}

(This script is complete, it should run "as is")

!!! note Keep in mind that pydantic.dataclasses.dataclass is a drop-in replacement for dataclasses.dataclass with validation, not a replacement for pydantic.BaseModel. There are cases where subclassing pydantic.BaseModel is the better choice.

For more information and discussion see
[samuelcolvin/pydantic#710](https://github.com/samuelcolvin/pydantic/issues/710).

You can use all the standard pydantic field types, and the resulting dataclass will be identical to the one created by the standard library dataclass decorator.

The underlying model and its schema can be accessed through __pydantic_model__. Also, fields that require a default_factory can be specified by a dataclasses.field.

{!.tmp_examples/dataclasses_default_schema.py!}

(This script is complete, it should run "as is")

pydantic.dataclasses.dataclass's arguments are the same as the standard decorator, except one extra keyword argument config which has the same meaning as Config.

!!! warning After v1.2, The Mypy plugin must be installed to type check pydantic dataclasses.

For more information about combining validators with dataclasses, see dataclass validators.

Nested dataclasses

Nested dataclasses are supported both in dataclasses and normal models.

{!.tmp_examples/dataclasses_nested.py!}

(This script is complete, it should run "as is")

Dataclasses attributes can be populated by tuples, dictionaries or instances of the dataclass itself.

Initialize hooks

When you initialize a dataclass, it is possible to execute code after validation with the help of __post_init_post_parse__. This is not the same as __post_init__, which executes code before validation.

{!.tmp_examples/dataclasses_post_init_post_parse.py!}

(This script is complete, it should run "as is")

Since version v1.0, any fields annotated with dataclasses.InitVar are passed to both __post_init__ and __post_init_post_parse__.

{!.tmp_examples/dataclasses_initvars.py!}

(This script is complete, it should run "as is")