question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support ForwardRef in Python 3.6

See original GitHub issue

Currently pydantic only supports ForwardRef in Python 3.7. It will be great if it can also be supported in Python 3.6. Although a ForwardRef can’t be explicitly created in Python 3.6, it can be implicitly created:

class A(BaseModel):
    a: 'A' = None

If you take a look at the ForwardRef class in Python 3.6 and Python 3.7, they share a very similar interface. The only major difference I spotted is that in python 3.6, ForwardRef is named as _ForwardRef and the method _evaluate in python 3.7 is named as _eval_type in python 3.6.

Quickly looking through the code I believe we can change https://github.com/samuelcolvin/pydantic/blob/a704662ae4cde638c0e1b68126a2f89828787641/pydantic/utils.py#L24-L28 to

try: 
     from typing import ForwardRef  # type: ignore 
 except ImportError: 
     # python 3.6 
    from typing import _ForwardRef as ForwardRef

and https://github.com/samuelcolvin/pydantic/blob/a704662ae4cde638c0e1b68126a2f89828787641/pydantic/main.py#L456-L459 to

 for f in cls.__fields__.values(): 
     if type(f.type_) == ForwardRef: 
         f.type_ = f.type_._eval_type(globalns, localns or None)  # type: ignore 
         f.prepare() 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mooncake4132commented, Apr 6, 2019

Thanks. I’ll prepare a PR once https://github.com/samuelcolvin/pydantic/pull/464 lands.

0reactions
samuelcolvincommented, Oct 25, 2019

Done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support ForwardRef in Python 3.6 · Issue #463 - GitHub
Currently pydantic only supports ForwardRef in Python 3.7. It will be great if it can also be supported in Python 3.6.
Read more >
How to pass ForwardRef as args to TypeVar in Python 3.6?
Here's my current workaround I'm using to support Python 3.6. This isn't pretty but it seems to at least get the code to...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar...
Read more >
[Example code]-How to pass ForwardRef as args to TypeVar in ...
I'm working on a library that currently supports Python 3.6+, ... I can use to support ForwardRef types as arguments to TypeVar or...
Read more >
pydantic — pydantic v0.28 documentation
Both postponed annotations via the future import and ForwardRef require python 3.7+. Support for those features starts from pydantic v0.18. Postponed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found