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 ForwardRef in Python 3.6 #463

Closed
mooncake4132 opened this issue Apr 5, 2019 · 9 comments
Closed

Support ForwardRef in Python 3.6 #463

mooncake4132 opened this issue Apr 5, 2019 · 9 comments
Labels
feature request help wanted Pull Request welcome

Comments

@mooncake4132
Copy link

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

Humm, I'm not completely opposed to it, but why not just upgrade to 3.7?

3.7 has been released and stable for some time now, the chance of a bugs from upgrading is tiny, the only reason I can imagine is that you can't upgrade, eg. your tied to an OS with 3.6.

@mooncake4132
Copy link
Author

Unfortunately we are not able to upgrade to 3.7 due to some of our dependencies (i.e. tensorflow and some projects that used the name async)

@samuelcolvin samuelcolvin added the help wanted Pull Request welcome label Apr 5, 2019
@samuelcolvin
Copy link
Member

humm, shame.

Ok, PR welcome to all ForwardRef with 3.6.

@mooncake4132
Copy link
Author

Thanks. I'll prepare a PR once #464 lands.

@gangefors
Copy link
Contributor

gangefors commented May 22, 2019

@mooncake4132 Do you have a PR for this in the works? I'm in a place where we want to support both py36 and py37 using ForwardRef.

@mooncake4132
Copy link
Author

@gangefors I'm currently in a situation where I don't easily have internet connection everyday. Feel free to grab it if you need it now.

@xNinjaKittyx
Copy link

xNinjaKittyx commented Jun 13, 2019

I also would love this feature. Even though the technical problems to upgrade to 3.7 may be few, there's plenty of corporate reasons why you wouldn't be able to do so.

@samuelcolvin
Copy link
Member

PR welcome.

@samuelcolvin
Copy link
Member

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

No branches or pull requests

4 participants