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
feat: support generic types and | operator #2609
Conversation
4ec7324
to
02c8987
Compare
I'm assuming this is still a work in progress? let me know when you want me to review it. (please update (just for the bot to assign you 😄)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just had a quick read of this, it looks great to me.
setup.py
Outdated
@@ -133,6 +133,7 @@ def extra(self): | |||
extras_require={ | |||
'email': ['email-validator>=1.0.3'], | |||
'dotenv': ['python-dotenv>=0.10.4'], | |||
'future': ['future-typing>=0.4.0'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we go with this, I think we'll need a more clear name for this extra.
else: | ||
# We need a merged version of typing (with typing_extensions) to | ||
# ensure we can resolve things like `Literal` or `Annotated` with older versions | ||
from . import typing_merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first I did something like
all_typing_mod_name = 'pydantic_all_typing___'
all_typing_mod = types.ModuleType(all_typing_mod_name)
exec("from typing import *\nfrom typing_extensions import *", all_typing_mod.__dict__)
But since using eval()
is problematic with cython
, I went with this approach.
I wrote some documentation. Feedback welcome! Please review |
pydantic models aren't yet compatible with Python 3.10 type annotations. See pydantic/pydantic#2597 and pydantic/pydantic#2609.
Let's focus on 3.10 and see if people actually want this. |
I was surprised to see that pydantic doesnt support the |
Hi @PrettyWood - I'd like to give a +1 in support of landing this if still possible. I think the support for unions & generics will cover 80+% of cases; it'd be nice to write Forward refs in particular have always been a little bit of a thorn (even with the stdlib), and so I don't think it needs to block something like this that supports the simpler cases. |
@tuchandra If you use 3.10, it's already supported. This PR is to have a workaround for python 3.7+ using |
@PrettyWood sorry, I wasn't clear - yeah, I'm on 3.8/3.9 but I understand not merging it. Thanks for the reply! |
This commit will update type annotation syntax for Python 3.10. The project currently also supports Python 3.8 and 3.9, so the annotations are imported with `from __future__ import annotations`. The Python 3.10 union operator (the pipe, like `str | None`) will not be used on pydantic models. If running Python 3.9 or below, pydantic is not compatible with the union operator, even if annotations are imported with `from __future__ import annotations`. https://peps.python.org/pep-0604/ https://docs.python.org/3/whatsnew/3.10.html pydantic/pydantic#2597 (comment) pydantic/pydantic#2609 (comment) pydantic/pydantic#3300 (comment)
agreed |
I understand not merging as well but I would certainly love to see something like this actually get merged. Its not great but unfortunately I have systems stuck with 3.8 for the foreseeable future and it would be nice to use the newer syntax. It could also be useful for packages that use Pydantic which still support older versions of python as well. But honestly, it probably better to focus on the future. |
I really think this would have been too confusing for users, we were right to drop it. On a related note, see #4339. |
Change Summary
Now this is possible with python 3.7+
Related issue number
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)