When a non-descriptor callable (such as a lambda or a class instance) is registered as a handler, singledispatchmethod unconditionally attempts to call the __get__ method on that handler during dispatch.
This results in an AttributeError, because non-descriptor callables do not have a get method.
This behavior directly violates the explicit guarantee in singledispatchmethod's own docstring, which states that it does handle non-descriptor callables.
importfunctoolsclassMyCallable:
def__call__(self, host_self, arg):
returnf"Handled {arg} via MyCallable"classHost:
@functools.singledispatchmethoddefmy_method(self, arg):
returnf"Base handling for {arg}"callable_instance=MyCallable()
Host.my_method.register(int, callable_instance)
h=Host()
h.my_method(123)
changed the title [-]`functools.singledispatchmethod` crashes with `AttributeError` for registered non-descriptor callables, violating documentation guarantee[/-][+]Allow non-descriptor callables in `functools.singledispatchmethod`[/+]on Nov 2, 2025
I'll categorize this as a new feature as this is the main point of this issue (and considering no one complained until now I think we can safely assume that it wasn't assumed so).
But we need to correct the docstring otherwise (as part of this issue). Adding support for non-descriptors would possibly break existing code so better to consider this a new feature.
Although we have several tests for non-descriptor callable (I think they were added by me). They are passed because the methods were not called, we only tested other their properties.
Activity
pythongh-140873: Add support of non-descriptor callables in functools…
serhiy-storchaka commented on Nov 1, 2025
This never worked, so it can be considered a new feature.
The docstring should be corrected in 3.14 and older.
picnixz commented on Nov 2, 2025
I'll categorize this as a new feature as this is the main point of this issue (and considering no one complained until now I think we can safely assume that it wasn't assumed so).
But we need to correct the docstring otherwise (as part of this issue). Adding support for non-descriptors would possibly break existing code so better to consider this a new feature.
serhiy-storchaka commented on Nov 2, 2025
Although we have several tests for non-descriptor callable (I think they were added by me). They are passed because the methods were not called, we only tested other their properties.
gh-140873: Add support of non-descriptor callables in functools.singl…
pythongh-140873: Fix the singledispatchmethod documentation
Merge branch 'main' into fix-issue21914
Merge branch 'main' of github.com:python/cpython into fix-Issue18387
Merge branch 'main' of github.com:python/cpython into fix-issue24255
[3.14] gh-140873: Fix the singledispatchmethod documentation (GH-141523)
[3.14] pythongh-140873: Fix the singledispatchmethod documentation (p…
[3.13] gh-140873: Fix the singledispatchmethod documentation (GH-141523…
pythongh-140873: Add support of non-descriptor callables in functools…