fix: pydantic dataclass can inherit from stdlib dataclass and arbitrary_types_allowed is supported (#2051)

* fix: pydantic dataclasses can inherit from stdlib dataclasses

closes #2042

* docs: add some documentation

* fix: support arbitrary_types_allowed with stdlib dataclass

closes #2054

* docs: add documentation for custom types
This commit is contained in:
Eric Jolibois
2020-10-28 20:10:55 +01:00
committed by GitHub
parent e899692654
commit 0b9cd4e537
7 changed files with 143 additions and 6 deletions
+25
View File
@@ -49,6 +49,8 @@ Dataclasses attributes can be populated by tuples, dictionaries or instances of
## Stdlib dataclasses and _pydantic_ dataclasses
### Convert stdlib dataclasses into _pydantic_ dataclasses
Stdlib dataclasses (nested or not) can be easily converted into _pydantic_ dataclasses by just decorating
them with `pydantic.dataclasses.dataclass`.
@@ -57,6 +59,18 @@ them with `pydantic.dataclasses.dataclass`.
```
_(This script is complete, it should run "as is")_
### Inherit from stdlib dataclasses
Stdlib dataclasses (nested or not) can also be inherited and _pydantic_ will automatically validate
all the inherited fields.
```py
{!.tmp_examples/dataclasses_stdlib_inheritance.py!}
```
_(This script is complete, it should run "as is")_
### Use of stdlib dataclasses with `BaseModel`
Bear in mind that stdlib dataclasses (nested or not) are **automatically converted** into _pydantic_ dataclasses
when mixed with `BaseModel`!
@@ -65,6 +79,17 @@ when mixed with `BaseModel`!
```
_(This script is complete, it should run "as is")_
### Use custom types
Since stdlib dataclasses are automatically converted to add validation using
custom types may cause some unexpected behaviour.
In this case you can simply add `arbitrary_types_allowed` in the config!
```py
{!.tmp_examples/dataclasses_arbitrary_types_allowed.py!}
```
_(This script is complete, it should run "as is")_
## Initialize hooks
When you initialize a dataclass, it is possible to execute code *after* validation