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

@samuelcolvin
Copy link
Owner

@samuelcolvin 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
Copy link

@codecov 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 issue in demosdemon/pydantic Dec 30, 2018
pydantic/fields.py Outdated Show resolved Hide resolved
@samuelcolvin
Copy link
Owner Author

@samuelcolvin 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.

tests/test_py37.py Outdated Show resolved Hide resolved
@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
9 checks passed
@samuelcolvin samuelcolvin deleted the postponed-annotations branch Jan 11, 2019
@StephenBrown2
Copy link
Collaborator

@StephenBrown2 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
Copy link
Owner Author

@samuelcolvin samuelcolvin commented Jan 23, 2019

Thanks, great to hear.

@theenglishway
Copy link
Contributor

@theenglishway theenglishway commented Apr 23, 2019

Works for me as well, although with a slight catch compared to @StephenBrown2

(Python 3.7.3rc1, Pydantic 0.23)

I have this model :

class Entity(BaseModel):
    name: str
    relatives: List[Entity] = []

If I only add from __future__ import annotations, pydantic yells with the (very informative) message : pydantic.errors.ConfigError: field "relatives" not yet prepared so type is still a ForwardRef, you might need to call Entity.update_forward_refs().

Obviously adding that piece of code just works 👍

I've had some little issues with the documentation regarding this feature though, for which I'm making an issue and a PR.

@samuelcolvin
Copy link
Owner Author

@samuelcolvin samuelcolvin commented Apr 23, 2019

great to hear it works, PR for docs very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants