-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pydantic dataclass can inherit from stdlib dataclass and `arbitr…
…ary_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
- Loading branch information
1 parent
e899692
commit 0b9cd4e
Showing
7 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fix: _pydantic_ `dataclass` can inherit from stdlib `dataclass` | ||
and `Config.arbitrary_types_allowed` is supported |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import dataclasses | ||
|
||
import pydantic | ||
|
||
|
||
class ArbitraryType: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
def __repr__(self): | ||
return f'ArbitraryType(value={self.value!r})' | ||
|
||
|
||
@dataclasses.dataclass | ||
class DC: | ||
a: ArbitraryType | ||
b: str | ||
|
||
|
||
# valid as it is a builtin dataclass without validation | ||
my_dc = DC(a=ArbitraryType(value=3), b='qwe') | ||
|
||
try: | ||
class Model(pydantic.BaseModel): | ||
dc: DC | ||
other: str | ||
|
||
Model(dc=my_dc, other='other') | ||
except RuntimeError as e: # invalid as it is now a pydantic dataclass | ||
print(e) | ||
|
||
|
||
class Model(pydantic.BaseModel): | ||
dc: DC | ||
other: str | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
|
||
m = Model(dc=my_dc, other='other') | ||
print(repr(m)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters