You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
future annotations with Python 3.8 cause TypeError #7873
I feel like I'm missing something...is this just expected to work? Even with __future__ import annotations, pydantic needs to actually evaluate int | str at runtime, and it's completely normal that before Python 3.10 this raises an error. This is a limitation of older versions of Python for any program that needs type hints at runtime, not just pydantic. Same for using list[str] instead of typing.List[str].
Maybe this could be made to work, but it seems more like a feature request than a bug.
The solution I'm imagining is modifying the AST to replace | with Union before evaluating. I think it'll work, I'm just not sure if it's worth the complexity to make it slightly more convenient to use older Python versions.
The solution I'm imagining is modifying the AST to replace | with Union before evaluating. I think it'll work, I'm just not sure if it's worth the complexity to make it slightly more convenient to use older Python versions.
Yes, Union will work. This is a way I discover this issue try migrate from Union to | for Python 3.8 using future import.
I feel like I'm missing something...is this just expected to work? Even with __future__ import annotations, pydantic needs to actually evaluate int | str at runtime, and it's completely normal that before Python 3.10 this raises an error. This is a limitation of older versions of Python for any program that needs type hints at runtime, not just pydantic. Same for using list[str] instead of typing.List[str].
Maybe this could be made to work, but it seems more like a feature request than a bug.
Activity
sydney-runkle commentedon Oct 19, 2023
Hi @emcek,
Thanks for reporting this! I was able to replicate this issue. Indeed seems like a bug 🐛. We will look into a fix!
Parici75 commentedon Oct 20, 2023
I can confirm I have the same bug when using Python 3.9.
Python, Pydantic & OS Version
alexmojaki commentedon Oct 20, 2023
I feel like I'm missing something...is this just expected to work? Even with
__future__ import annotations, pydantic needs to actually evaluateint | strat runtime, and it's completely normal that before Python 3.10 this raises an error. This is a limitation of older versions of Python for any program that needs type hints at runtime, not just pydantic. Same for usinglist[str]instead oftyping.List[str].Maybe this could be made to work, but it seems more like a feature request than a bug.
alexmojaki commentedon Oct 21, 2023
The solution I'm imagining is modifying the AST to replace
|withUnionbefore evaluating. I think it'll work, I'm just not sure if it's worth the complexity to make it slightly more convenient to use older Python versions.emcek commentedon Oct 21, 2023
Yes,
Unionwill work. This is a way I discover this issue try migrate fromUnionto|for Python 3.8 usingfutureimport.sydney-runkle commentedon Oct 23, 2023
@alexmojaki @emcek,
I'm inclined to think this is a bug with Pydantic due to PEP 563 and the pydantic docs on postponed annotations.
alexmojaki commentedon Oct 23, 2023
OK, then I'll take this one.
sydney-runkle commentedon Oct 23, 2023
@alexmojaki Awesome! I'll take a look at your PRs when I have a chance today / tmrw. Thanks for all of the contributions 😄.
emcek commentedon Oct 23, 2023
@alexmojaki ping me when PR is ready I would love to check solution...
PrettyWood commentedon Oct 24, 2023
I agree. This is why I made a POC+ in https://github.com/PrettyWood/future-typing but it still needs some work and IMO is not worth the time. I don't see how it should work without that
alexmojaki commentedon Oct 24, 2023
What I'm thinking is transforming the AST and then compiling and evaluating that directly, which should be easier than manipulating tokens.
PrettyWood commentedon Oct 24, 2023
Ah yes ! I'm still stuck in v1 where we depended on cython that made evaluation impossible
alexmojaki commentedon Nov 4, 2023
PR ready: #7958
patch pydantic runtime type support
Remove Python 3.8 ci, see pydantic/pydantic/issues/7873
devel/py-eval-type-backport: Add py-eval-type-backport 0.2.2
devel/py-eval-type-backport: Add py-eval-type-backport 0.2.2
devel/py-eval-type-backport: Add py-eval-type-backport 0.2.2
devel/py-eval-type-backport: Add py-eval-type-backport 0.2.2