Skip to content

Allow non-descriptor callables in functools.singledispatchmethod #140873

@T90REAL

Description

@T90REAL

Bug report

Bug description:

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.

import functools

class MyCallable:
    def __call__(self, host_self, arg):
        return f"Handled {arg} via MyCallable"

class Host:
    @functools.singledispatchmethod
    def my_method(self, arg):
        return f"Base handling for {arg}"

callable_instance = MyCallable()
Host.my_method.register(int, callable_instance)


h = Host()

h.my_method(123)
AttributeError: 'MyCallable' object has no attribute '__get__'. Did you mean: '__ge__'?

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions