Skip to content

Allow non-descriptor callables in functools.singledispatchmethod #140873

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Nov 1, 2025
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 1, 2025
  3. added a commit that references this issue on Nov 1, 2025
  4. serhiy-storchaka commented on Nov 1, 2025

    @serhiy-storchaka
    Member

    This never worked, so it can be considered a new feature.

    The docstring should be corrected in 3.14 and older.

  5. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Nov 2, 2025
  6. 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
  7. picnixz commented on Nov 2, 2025

    @picnixz
    Member

    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.

  8. serhiy-storchaka commented on Nov 2, 2025

    @serhiy-storchaka
    Member

    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.

  9. added 2 commits that reference this issue on Nov 13, 2025
  10. added a commit that references this issue on Nov 18, 2025
  11. added a commit that references this issue on Nov 18, 2025
  12. added a commit that references this issue on Nov 18, 2025
  13. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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