Skip to content
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

Merged
merged 6 commits into from
Jun 14, 2019
Merged

Conversation

pfrederiks
Copy link
Contributor

@pfrederiks pfrederiks commented Jun 13, 2019

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

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • include your github username @<whomever>

@codecov
Copy link

codecov bot commented Jun 13, 2019

Codecov Report

Merging #591 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #591   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2542   2542           
  Branches      504    504           
=====================================
  Hits         2542   2542

Copy link
Member

@samuelcolvin samuelcolvin left a 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 Show resolved Hide resolved
tests/test_dataclasses.py Outdated Show resolved Hide resolved
tests/test_dataclasses.py Outdated Show resolved Hide resolved
@pydantic.dataclasses.dataclass
class RadiansWithInitVar:
rad: float = dataclasses.field(init=False)
deg: InitVar
Copy link
Member

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]

Copy link
Contributor Author

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.

Copy link
Member

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).

Copy link
Contributor Author

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__.

tests/test_dataclasses.py Outdated Show resolved Hide resolved
@samuelcolvin samuelcolvin merged commit b77da1e into pydantic:master Jun 14, 2019
somada141 added a commit to somada141/pydantic that referenced this pull request Jun 14, 2019
@samuelcolvin
Copy link
Member

thanks a lot, much appreciated.

@pfrederiks pfrederiks deleted the support-initvars branch October 14, 2023 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants