Message379896
It's worth pointing out that a similar error is produced for a forward-referenced return type of a registered method, but only for python3.9. For example:
from __future__ import annotations
from functools import singledispatchmethod
class Integer:
def __init__(self, value: int):
self.value = value
def __str__(self) -> str:
return str(self.value)
@singledispatchmethod
def add(self, other: object) -> Integer:
raise NotImplementedError(f"Unsupported type {type(other)}")
@add.register
def _(self, other: int) -> "Integer":
return Integer(self.value + other)
print(Integer(2).add(40))
This code runs without error in python3.8, and I am using this technique in code running in a production environment.
$ python3.8 --version
Python 3.8.6
$ python3.8 integer.py
42
However, this code throws a NameError in python3.9.
$ python3.9 --version
Python 3.9.0
$ python3.9 integer.py
Traceback (most recent call last):
File "/Users/ryansobol/Downloads/integer.py", line 5, in <module>
class Integer:
File "/Users/ryansobol/Downloads/integer.py", line 17, in Integer
def _(self, other: int) -> "Integer":
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/functools.py", line 909, in register
return self.dispatcher.register(cls, func=method)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/functools.py", line 860, in register
argname, cls = next(iter(get_type_hints(func).items()))
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 1386, in get_type_hints
value = _eval_type(value, globalns, localns)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 254, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 497, in _evaluate
self.__forward_value__ = _eval_type(
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 254, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 493, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'Integer' is not defined
I know that some may see this issue as a feature request for 3.10+. However, for me, it is a bug preventing my code from migrating to 3.9. |
|
Date |
User |
Action |
Args |
2020-10-29 22:51:46 | ryansobol | set | recipients:
+ ryansobol, gvanrossum, glyph, ethan smith, BTaskaya |
2020-10-29 22:51:46 | ryansobol | set | messageid: <1604011906.39.0.0159967456788.issue41987@roundup.psfhosted.org> |
2020-10-29 22:51:46 | ryansobol | link | issue41987 messages |
2020-10-29 22:51:46 | ryansobol | create | |
|