Support ForwardRef in Python 3.6
See original GitHub issueCurrently 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
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:
- Created 4 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks. I’ll prepare a PR once https://github.com/samuelcolvin/pydantic/pull/464 lands.
Done.