question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider not applying PEP 604 by presence of __future__

See original GitHub issue

Applying 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
asottilecommented, Feb 1, 2021

#389 probably does what you want

0reactions
antonagestamcommented, Feb 1, 2021

@asottile Hey, thanks for reconsidering and fixing! 🙏

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found