Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change pydantic dataclass decorator (#2557)
* refactor: rewrite the whole pydantic dataclass logic * test: add tests for issue 2162 * test: add tests for issue 2383 * test: add tests for issue 2398 * test: add tests for issue 2424 * test: add tests for issue 2541 * test: add tests for issue 2555 * refactor: polish * change default and support 3.6 * fix coverage * fix mypy and text * typos * test: add tests for issue 2594 * fix: forward doc for schema description * add change * chore: small changes from review * refactor: avoid extra __pydantic_run_validation__ parameter * small tweaks * remove wrapper * support 3.6 * fix: mypy * rewrite doc * add docs * wrapper is removed now * a bit more docs * code review * faster dict update * add test for issue 3162 * add test for issue 3011 * feat: add `Config.post_init_after_validation` * allow config via dict * fix cython and TypedDict * chore: typo * move `compiled` in `version.py` * refactor: switch from `Config.post_init_after_validation` to \'post_init_call` * add dataclass isinstance support * avoid multi paragraphs in change file * feat: support `Config.extra` * refactor: simplify a bit code * refactor: avoid creating useless functions * refactor: simplify `is_builtin_dataclass` * support extra in post_init * docs: add warning on config extra * fix #3713 compatibility * update docs Co-authored-by: Samuel Colvin <s@muelcolvin.com>
- Loading branch information
1 parent
bc96cc9
commit 576e4a3
Showing
17 changed files
with
872 additions
and
240 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,9 @@ | ||
Refactor the whole _pydantic_ `dataclass` decorator to really act like its standard lib equivalent. | ||
It hence keeps `__eq__`, `__hash__`, ... and makes comparison with its non-validated version possible. | ||
It also fixes usage of `frozen` dataclasses in fields and usage of `default_factory` in nested dataclasses. | ||
The support of `Config.extra` has been added. | ||
Finally, config customization directly via a `dict` is now possible. | ||
<br/><br/> | ||
**BREAKING CHANGES** | ||
- The `compiled` boolean (whether _pydantic_ is compiled with cython) has been moved from `main.py` to `version.py` | ||
- Now that `Config.extra` is supported, `dataclass` ignores by default extra arguments (like `BaseModel`) |
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,26 @@ | ||
from pydantic import ConfigDict | ||
from pydantic.dataclasses import dataclass | ||
|
||
|
||
# Option 1 - use directly a dict | ||
# Note: `mypy` will still raise typo error | ||
@dataclass(config=dict(validate_assignment=True)) | ||
class MyDataclass1: | ||
a: int | ||
|
||
|
||
# Option 2 - use `ConfigDict` | ||
# (same as before at runtime since it's a `TypedDict` but with intellisense) | ||
@dataclass(config=ConfigDict(validate_assignment=True)) | ||
class MyDataclass2: | ||
a: int | ||
|
||
|
||
# Option 3 - use a `Config` class like for a `BaseModel` | ||
class Config: | ||
validate_assignment = True | ||
|
||
|
||
@dataclass(config=Config) | ||
class MyDataclass3: | ||
a: int |
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,30 @@ | ||
import dataclasses | ||
|
||
from pydantic import ValidationError | ||
from pydantic.dataclasses import dataclass as pydantic_dataclass, set_validation | ||
|
||
|
||
@dataclasses.dataclass | ||
class User: | ||
id: int | ||
name: str | ||
|
||
|
||
# Enhance stdlib dataclass | ||
pydantic_dataclass(User) | ||
|
||
|
||
user1 = User(id='whatever', name='I want') | ||
|
||
# validate data of `user1` | ||
try: | ||
user1.__pydantic_validate_values__() | ||
except ValidationError as e: | ||
print(e) | ||
|
||
# Enforce validation | ||
try: | ||
with set_validation(User, True): | ||
User(id='whatever', name='I want') | ||
except 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
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
Oops, something went wrong.