Skip to content

Commit

Permalink
docs: add some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Oct 27, 2020
1 parent 471fc74 commit 4a0dc9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/examples/dataclasses_stdlib_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import dataclasses

import pydantic


@dataclasses.dataclass
class Z:
z: int


@dataclasses.dataclass
class Y(Z):
y: int = 0


@pydantic.dataclasses.dataclass
class X(Y):
x: int = 0

foo = X(x=b'1', y='2', z='3')
print(foo)

try:
X(z='pika')
except pydantic.ValidationError as e:
print(e)
8 changes: 8 additions & 0 deletions docs/usage/dataclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ them with `pydantic.dataclasses.dataclass`.
```
_(This script is complete, it should run "as is")_

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")_

Bear in mind that stdlib dataclasses (nested or not) are **automatically converted** into _pydantic_ dataclasses
when mixed with `BaseModel`!

Expand Down

0 comments on commit 4a0dc9e

Please sign in to comment.