Consider not applying PEP 604 by presence of __future__
See original GitHub issueApplying PEP 604 on non 3.10 code will crash on libraries depending on introspection of type hints using typing.get_type_hints
. This should apply for libraries like https://github.com/samuelcolvin/pydantic and https://github.com/konradhalas/dacite.
Example:
>>> from __future__ import annotations
>>> import typing
>>> class Snake(typing.NamedTuple):
... name: str | None
...
>>> typing.get_type_hints(Snake)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 1410, in get_type_hints
value = _eval_type(value, base_globals, localns)
File "/usr/local/Cellar/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 277, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
File "/usr/local/Cellar/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 533, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
class Snake(typing.NamedTuple):
name: str | None
My proposal is to only rewrite to PEP 604 when the minimum version is 3.10.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
PEP 604 error with TypedDict not recognized #918 - GitHub
Actual behaviour. The code sample below causes two TypeErrors to be thrown even with from __future__ import annotations . However, pylance only ...
Read more >PEP 604 – Allow writing union types as X | Y
To describe a disjunction (union type), the user must use Union[X, Y] . The verbosity of this syntax does not help with type...
Read more >Support PEP 604 -- Allow writing union types as X | Y
Union for generated type hints (intentions for annotating a variable). Properly take into account the presence of from __future__ import annotations . Ensure ......
Read more >How does one determine the minimum patch version of ...
I am a maintainer for a Python library that is considering supporting PEP 604, now in Python3.10. Our library currently supports Python3.8+ ...
Read more >PEP to Prevent HIV Infection - NCBI Bookshelf - NIH
The first dose of PEP should be administered within 2 hours of an ... A bite is not considered a risk exposure to...
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 FreeTop 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
Top GitHub Comments
#389 probably does what you want
@asottile Hey, thanks for reconsidering and fixing! 🙏