Repository navigation
singledispatchmethod raises an error when relying on a forward declaration #86153
Description
Activity
This example:
from __future__ import annotations from functools import singledispatchmethod class Comparable: @singledispatchmethod def compare(self, arg: object): raise NotImplementedError("what") @compare.register def _(self, arg: Comparable): return "somewhat similar"
print(Comparable().compare(Comparable()))
Produces this result:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/typing.py", line 518, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'Comparable' is not definedIt seems like perhaps singledispatchmethod should defer its type evaluation to its first invocation?
- added3.8 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Oct 9, 2020 AFAIK the normal way of registering types (dispatcher.register(<type>)) also requires that registered type be defined at the execution type. I guess, if we are going to support such a thing, we might end up with supporting passing strings into the .register() as the initial argument of matching type to be consistent. Anyways, this would be a feature request for 3.10+, so changing the version info.
Reacted by Bartosz SławeckiThis behavior (only relevant with
from __future__ import annotations) has been around since @singledispatchmethod was introduced in 3.8, so I agree we should treat it as a feature request.In the meantime, maybe a workaround is to move the register call out of the class? It looks a little bit ugly but probably works.
(Disclaimer: I'm not familiar with singledispatchmethod.)
The behavior is the same with a traditional quoted forward declaration, so it’s not specific to the __future__ import; I just phrased the example that way to show how it’s going to look in the future and to illustrate how it might crop up in a way which is maximally confusing to users less familiar with the internals of type annotations.
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.
11 remaining items
Hello!
git-bisect points at
https://bugs.python.org/issue41341
#21553It breaks both the examples from
https://bugs.python.org/issue41987#msg379896 and
https://bugs.python.org/issue41987#msg380803Thanks for the bisection. It's not surprising that that's the culprit, and in other situations that's the right thing to do. I'm not sure how to address this without breaking other stuff -- maybe leave the ForwardRef if evaluating it doesn't work? But that's likely to have other subtle side effects -- we still want simple typos (or other reasons why a reference is legitimately broken) to go unchecked. Maybe singledispatch can catch the error and fall back on looking at bare __annotations__?
This can probably be closed as it's not an issue in Py 3.10+
This can probably be closed as it's not an issue in Py 3.10+
I can still reproduce the issue, so I can't confirm this. What exactly changed in Python 3.10?
First, regarding the problem discussed in #86153 (comment) and further on: this remained unfixed in 3.9, 3.10, 3.11, 3.12 and 3.13. It was fixed by PEP 749, precisely by 7b7b90d (GH-119891):
t.py:from __future__ import annotations from functools import singledispatchmethod class Integer: @singledispatchmethod def add(self, other: object) -> Integer: raise NotImplementedError(f"Unsupported type {type(other)}") @add.register def _(self, other: int) -> "Integer": ... print("ok!")
❯ uvx every-python run 7b7b90d~ python t.py |& tail -n 1 NameError: name 'Integer' is not defined ❯ uvx every-python run 7b7b90d python t.py |& tail -n 1 ok!
Therefore, this is no longer a bug to fix.
The original error reported by the OP still reproduces on 3.15:
from __future__ import annotations from functools import singledispatchmethod class Comparable: @singledispatchmethod def compare(self, arg: object): raise NotImplementedError("what") @compare.register def _(self, arg: Comparable): return "somewhat similar"
with a lovely error message:
Traceback (most recent call last): File "/home/bswck/Python/cpython/t.py", line 33, in <module> class Comparable: ...<6 lines>... return "somewhat similar" File "/home/bswck/Python/cpython/t.py", line 38, in Comparable @compare.register ^^^^^^^^^^^^^^^^ File "/home/bswck/Python/cpython/Lib/functools.py", line 1030, in register return self.dispatcher.register(cls, func=method) ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "/home/bswck/Python/cpython/Lib/functools.py", line 974, in register raise TypeError( ...<2 lines>... ) TypeError: Invalid annotation for 'arg'. ForwardRef('Comparable') is an unresolved forward reference.
(Without
from __future__ import annotationson top we also get nearly the same error).It's not a bug report, but a feature request, as previously noted by @isidentical in #86153 (comment) and by @gvanrossum in #86153 (comment). I'm relabeling this issue accordingly.
As @isidentical noted, the reason why this didn't work in the first place is that
@registerevaluates the annotation immediately, and at the time of evaluation the class body is still executing to complete the class namespace; the final class object is not available in any way yet. There is no way to get to the class object, because it simply doesn't exist at this time.One simple solution that comes to my mind is delaying registrations entirely by moving them to
__set_name__ofsingledispatchmethod, when the class object is ready.
singledispatchmethod.register()would simply queue registrations and__set_name__would flush it to the singledispatch registry in the context of the finalized class.Doing this to all registrations could break
.dispatchcalls inside the class body, but that seems like poor programming to me.
If that is a concern though, we could only queue unresolvable registrations, but that gives us a complicated flow to reason about + implementation complexity.- addedtype-featureA feature request or enhancementA feature request or enhancementand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error3.10 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life
on Jan 6, 2026 Revisiting this, I think it's not worth it.
This is easy and works:
from __future__ import annotations from functools import singledispatchmethod class Comparable: @singledispatchmethod def compare(self, arg: object): raise NotImplementedError("what") def callback(self, arg: Comparable): return "somewhat similar" Comparable.compare.register(Comparable.callback)
The cost of changing the assumptions that singledispatch was built on is IMO too high to support this one edge case.
@johnslavik the suggested workaround:
from __future__ import annotations from functools import singledispatchmethod class Comparable: @singledispatchmethod def compare(self, arg: object): raise NotImplementedError("what") def callback(self, arg: Comparable): return "somewhat similar" Comparable.compare.register(Comparable.callback)
does work at runtime, but results in a
mypyerror:error: "Callable[..., Any]" has no attribute "register" [attr-defined]
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: