-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add support for InitVar #591
Conversation
Codecov Report
@@ Coverage Diff @@
## master #591 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 15 15
Lines 2542 2542
Branches 504 504
=====================================
Hits 2542 2542 |
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.
Good start, just need to simplify tests so where possible they test just one thing.
Also need to update HISTORY.rst.
tests/test_dataclasses.py
Outdated
@pydantic.dataclasses.dataclass | ||
class RadiansWithInitVar: | ||
rad: float = dataclasses.field(init=False) | ||
deg: InitVar |
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.
please also add a test for parameterised InitVar
, eg. InitVar[int]
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.
I will test this. The type will be ignored by pydantic though. Init vars are passed to post_init and not part of the model. I could add a type check in pydantic_post_init, but that could turn out to be more complicated than the original fix.
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.
no need to check the type, we're really just testing that field.type is not dataclasses.InitVar
still works (I think it won't, but could be wrong).
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.
field.type is not dataclasses.InitVar still worked, but now I have chosen to use dataclasses.fields instead. This also skips Class variables annotated with typing.ClassVar and it is a public function instead of the private __dataclasses_fields__
.
Add support for InitVar (pydantic#591)
thanks a lot, much appreciated. |
Change Summary
dataclasses.InitVar were not handled correctly. This change does the following:
Pass fields typed as InitVar as argument to the original
__post_init__
so that they can be processed like in the python 3.7 dataclasses library.Exclude InitVar fields from the pydantic model as they are not part of the dataclass object.
Related issue number
#592
Checklist
HISTORY.rst
has been updated#<number>
@<whomever>