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

support postponed annotations and ForwardRef in python 3.7 #348

Merged
merged 7 commits into from Jan 11, 2019

Conversation

Projects
None yet
2 participants
@samuelcolvin
Copy link
Owner

samuelcolvin commented Dec 29, 2018

Change Summary

support postponed annotations in python 3.7

Related issue number

fix #234 partial solution use of ForwardRef for the model itself isn't yet supported. Since almost all of the field setup logic assumes the model already exists this will be much more complicated

ForwardRef should now be fully supported. If the ForwardRef can't evaluate when creating the Model you'll need to call Model.update_forward_refs() after it's been created.

Also note that the Model will need to be defined in the root of the module so it's included in globals, this is a limitation of get_type_hints it could be fixed in future by implementing our own version of get_type_hints but that's beyond the scope of this PR I think.

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

This comment has been minimized.

Copy link

codecov bot commented Dec 29, 2018

Codecov Report

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

@@          Coverage Diff          @@
##           master   #348   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          14     14           
  Lines        1824   1857   +33     
  Branches      356    364    +8     
=====================================
+ Hits         1824   1857   +33

@samuelcolvin samuelcolvin force-pushed the postponed-annotations branch from 3cc22e7 to 1145ae4 Dec 29, 2018

@samuelcolvin samuelcolvin changed the title support postponed annotations in python 3.7 support postponed annotations and ForwardRef in python 3.7 Dec 29, 2018

@samuelcolvin samuelcolvin force-pushed the postponed-annotations branch from 3b7e2a9 to 5ff33cd Dec 30, 2018

samuelcolvin referenced this pull request in demosdemon/pydantic Dec 30, 2018

Show resolved Hide resolved pydantic/fields.py Outdated
@samuelcolvin

This comment has been minimized.

Copy link
Owner Author

samuelcolvin commented Jan 7, 2019

Humm, so 5 people ask for this or similar, but no one is willing to take 20 minutes and test it or give any feedback. Not a great reflection on the open source community.

I guess I'll wait a couple more days and merge it.

Show resolved Hide resolved tests/test_py37.py Outdated

@samuelcolvin samuelcolvin force-pushed the postponed-annotations branch from 0280183 to de9a834 Jan 11, 2019

@samuelcolvin samuelcolvin merged commit 25b5d98 into master Jan 11, 2019

5 checks passed

codecov/project 100% (+0%) compared to 32d5cdc
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
pyup.io/safety-ci No dependencies with known security vulnerabilities.
Details

@samuelcolvin samuelcolvin deleted the postponed-annotations branch Jan 11, 2019

@StephenBrown2

This comment has been minimized.

Copy link
Contributor

StephenBrown2 commented Jan 22, 2019

Just ran into this issue, and found it works for me!

Pydantic 0.18
Python 3.7
Example:

from __future__ import annotations
from pydantic import BaseModel

class CalendarRule(BaseModel):
    id: int
    calendar: int
    children: t.List[CalendarRule]

the above no longer crashes when I import annotations from __future__.

Thanks for your work on this!

@samuelcolvin

This comment has been minimized.

Copy link
Owner Author

samuelcolvin commented Jan 23, 2019

Thanks, great to hear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.