Skip to content

Conversation

samuelcolvin
Copy link
Member

@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 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 changed the title support postponed annotations in python 3.7 support postponed annotations and ForwardRef in python 3.7 Dec 29, 2018
@samuelcolvin
Copy link
Member Author

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.

@samuelcolvin samuelcolvin force-pushed the postponed-annotations branch from 0280183 to de9a834 Compare January 11, 2019 19:43
@samuelcolvin samuelcolvin merged commit 25b5d98 into master Jan 11, 2019
@samuelcolvin samuelcolvin deleted the postponed-annotations branch January 11, 2019 21:01
@StephenBrown2
Copy link
Contributor

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
Member Author

Thanks, great to hear.

@theenglishway
Copy link
Contributor

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
Member Author

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

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
* add "strip = true" to release profile

* bump
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.

support for ForwardRef (forward references)
3 participants