Skip to content

Consider not applying PEP 604 by presence of __future__  #387

@hannseman

Description

@hannseman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions