New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: change pydantic dataclass decorator #2557
Merged
samuelcolvin
merged 51 commits into
pydantic:master
from
PrettyWood:refactor/dataclass-decorator
Aug 4, 2022
Merged
Changes from 26 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
665b5c9
refactor: rewrite the whole pydantic dataclass logic
PrettyWood b723e59
test: add tests for issue 2162
PrettyWood 914f3bb
test: add tests for issue 2383
PrettyWood 8706598
test: add tests for issue 2398
PrettyWood 2e15ef5
test: add tests for issue 2424
PrettyWood f3d68ce
test: add tests for issue 2541
PrettyWood 95f1d15
test: add tests for issue 2555
PrettyWood f5a5756
refactor: polish
PrettyWood 9ae2e30
change default and support 3.6
PrettyWood d7b2b27
fix coverage
PrettyWood 64dbc56
fix mypy and text
PrettyWood e86f8bb
typos
PrettyWood 4086193
test: add tests for issue 2594
PrettyWood dc717fe
fix: forward doc for schema description
PrettyWood 9799d80
add change
PrettyWood e7cbee1
chore: small changes from review
PrettyWood 538c7b3
refactor: avoid extra __pydantic_run_validation__ parameter
PrettyWood 3874661
Merge branch 'master' into refactor/dataclass-decorator
PrettyWood 9174d7a
small tweaks
PrettyWood adda021
remove wrapper
PrettyWood bf0f191
support 3.6
PrettyWood c3aa37d
fix: mypy
PrettyWood 461fa1b
rewrite doc
PrettyWood 77f4d6b
add docs
PrettyWood 263859c
wrapper is removed now
PrettyWood 066f23c
a bit more docs
PrettyWood 432ae04
Merge branch 'master' into refactor/dataclass-decorator
PrettyWood 1926e85
Merge branch 'master' into refactor/dataclass-decorator
PrettyWood e3a8f87
code review
PrettyWood 80da159
faster dict update
PrettyWood bde50bb
add test for issue 3162
PrettyWood eee2e26
add test for issue 3011
PrettyWood d101420
feat: add `Config.post_init_after_validation`
PrettyWood 04f7ea0
allow config via dict
PrettyWood 8e24502
fix cython and TypedDict
PrettyWood 84ec7b5
Merge branch 'master' into refactor/dataclass-decorator
PrettyWood a8237d8
chore: typo
PrettyWood 5c5d152
move `compiled` in `version.py`
PrettyWood 6715d4c
refactor: switch from `Config.post_init_after_validation` to \'post_i…
PrettyWood c6d8201
add dataclass isinstance support
PrettyWood 6914c04
avoid multi paragraphs in change file
PrettyWood 30b58f3
feat: support `Config.extra`
PrettyWood 0079d93
Merge branch 'master' into refactor/dataclass-decorator
PrettyWood fb77d0d
refactor: simplify a bit code
PrettyWood 1a60ab2
refactor: avoid creating useless functions
PrettyWood ffecfc2
refactor: simplify `is_builtin_dataclass`
PrettyWood 3a6ddf1
support extra in post_init
PrettyWood 5907f82
docs: add warning on config extra
PrettyWood 571609d
Merge branch 'master' into PrettyWood-refactor/dataclass-decorator
samuelcolvin 4810457
fix #3713 compatibility
samuelcolvin 0c7d578
update docs
samuelcolvin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,3 @@ | ||
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. |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. I wrote some code which wraps standard lib dataclasses using pydantic dataclasses when I want to call validation to achieve the same thing. Would love to see it implemented in the pydantic lib itself so we can allow users to set up their model however they like (i.e. without having to pass everything to the init func) and parse/validate when they are done.