Files
pydantic/docs/usage/dataclasses.md
T
Samuel Colvin 33b7d52d31 moving docs to mkdocs (#856)
* moving docs to mkdocs

* transfering readme to md and more

* fixing build

* splitting usage.md

* improving schema.md and index.md

* fix make_history.rst

* models intro

* working on data conversation and required fields

* more fixes to models.md

* list all standard types supported

* list of pydantic types

* tweaks

* update links in code

* Apply suggestions from code review

incorporate @dmontagu's suggestions.

Co-Authored-By: dmontagu <35119617+dmontagu@users.noreply.github.com>

* Apply suggestions from code review

more missed suggestions.

Co-Authored-By: dmontagu <35119617+dmontagu@users.noreply.github.com>

* Apply suggestions from code review

more corrects.

* cleanup

* Field order warning

* fix and regenerate benchmarks

* format examples better, cleanup

* improve schema mapping table

* correct highlighting file types in schema.md

* add redirects in javascript

* add logo
2019-10-07 17:19:01 +01:00

2.4 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.

{!./examples/ex_dataclasses.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.

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

!!! note As a side effect of getting pydantic dataclasses to play nicely with mypy the config argument will show as invalid in IDEs and mypy, use @dataclass(..., config=Config) # type: ignore as a workaround.

See [python/mypy#6239](https://github.com/python/mypy/issues/6239) for an explanation of why this is.

For information on validators with dataclasses see dataclass validators

Nested dataclasses

Nested dataclasses are supported both in dataclasses and normal models.

{!./examples/ex_nested_dataclasses.py!}

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

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

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.

{!./examples/ex_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__.

{!./examples/ex_post_init_post_parse_initvars.py!}

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