FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`singledispatchmethod` dispatches on first argument to instance-bindable method called from class level · Issue #143535 · python/cpython · GitHub

Repository navigation

singledispatchmethod dispatches on first argument to instance-bindable method called from class level #143535

Closed

Description

In this usage of singledispatchmethod:

from functools import singledispatchmethod

class C:
    @singledispatchmethod
    def d(self, x):
        return "base!"

    @d.register(int)
    def _(self, x):
        return "int!"

print(C.d(C(), 1))

This is what I expected in the output:

int!

But instead this was printed out:

base!

singledispatch and singledispatchmethod don't analyze and adapt argument semantics to the registered callable type, which makes them very nice and simple.

_singledispatchmethod_get always dispatches on args[0] regardless of whether it's bound or unbound, and if it didn't, that would break descriptor support; this facet is crucial for staticmethod (and classmethod) dispatches to work:

from functools import singledispatchmethod

class C:
    @singledispatchmethod
    @staticmethod
    def d(x):
        return "base!"

    @d.register(int)
    @staticmethod
    def s(x):
        return "int!"
    
print(C.d(1))  # int!

Our tests rely on this and possibly millions of projects rely on this.

I think we could document this explicitly (and backport to 3.13 and 3.14).


Found this while working on GH-143465.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    docsDocumentation in the Doc dir
    stdlibStandard Library Python modules in the Lib/ directory
    on Jan 8, 2026
  2. changed the title [-]`singledispatchmethod` dispatches on the `self` argument what invoked from class level[/-] [+]`singledispatchmethod` dispatches on the `self` argument when invoked from class level[/+] on Jan 8, 2026
  3. changed the title [-]`singledispatchmethod` dispatches on the `self` argument when invoked from class level[/-] [+]Document that `singledispatchmethod` dispatches on the `self` argument when invoked from class level[/+] on Jan 9, 2026
  4. added a commit that references this issue on Jan 9, 2026
  5. priyanshu2282-cyber commented on Jan 9, 2026

    Contributor

    I have prepared a doc fix and raising a PR in which I have documented the behaviour that singledispatchmethod always dispatches first argument during call, which fails the regular behaviour of x.f(a)=c.f(a).

  6. added 3 commits that reference this issue on Jan 9, 2026
  7. changed the title [-]Document that `singledispatchmethod` dispatches on the `self` argument when invoked from class level[/-] [+]`singledispatchmethod` dispatches on bound argument when invoked from class level[/+] on Feb 8, 2026
  8. johnslavik commented on Feb 8, 2026

    MemberAuthor

    I was wrong. This can be fixed, 100% low-risk.

  9. changed the title [-]`singledispatchmethod` dispatches on bound argument when invoked from class level[/-] [+]`singledispatchmethod` dispatches on first argument to instance-bindable method called from class level[/+] on Feb 8, 2026
  10. added
    type-bugAn unexpected behavior, bug, or error
    3.13only security fixes
    3.14bugs and security fixes
    and removed
    docsDocumentation in the Doc dir
    on Feb 8, 2026
  11. added a commit that references this issue on Feb 24, 2026
  12. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Feb 24, 2026
  13. ambv commented on Feb 24, 2026

    Contributor

    Reclassifying as feature for backward compat safety. Fixed by #144615.

  14. added a commit that references this issue on Feb 28, 2026
  15. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL