GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `singledispatchmethod` dispatches on first argument to instance-bindable method called from class level · Issue #143535 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
singledispatchmethod dispatches on first argument to instance-bindable method called from class level#143535
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@johnslavik
johnslavik
opened on Jan 8, 2026
Issue body actions

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

  • gh-143535: Document dispatch behavior of singledispatchmethod #143592
  • gh-143535: Dispatch on the second argument if generic method is instance-bindable #144615

Activity

  1. johnslavik
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jan 8, 2026
  2. github-project-automation
    added this to docs issueson Jan 8, 2026
  3. github-project-automation
    moved this to Todo in docs issueson Jan 8, 2026
  4. johnslavik
    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
  5. johnslavik
    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
  6. priyanshu2282-cyber
    added a commit that references this issue on Jan 9, 2026

    pythongh-143535: Document dispatch behavior of singledispatchmethod

    666a2a8
  7. priyanshu2282-cyber commented on Jan 9, 2026

    @priyanshu2282-cyber
    priyanshu2282-cyber
    on Jan 9, 2026
    Contributor
    More actions

    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).

  8. priyanshu2282-cyber
    added 3 commits that reference this issue on Jan 9, 2026

    pythongh-143535: Move singledispatchmethod note outside example block

    ea2fda1

    pythongh-143535: Clarify dispatch semantics of singledispatchmethod

    2834c45

    pythongh-143535: Fix indentation in singledispatchmethod docs

    e033b66
  9. johnslavik
    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
  10. johnslavik commented on Feb 8, 2026

    @johnslavik
    johnslavik
    on Feb 8, 2026
    MemberAuthor
    More actions

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

  11. johnslavik
    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
  12. johnslavik
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    and removed
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Feb 8, 2026
  13. ambv
    added a commit that references this issue on Feb 24, 2026

    gh-143535: Dispatch on the second argument if generic method is insta…

    97181bb
  14. ambv
    added
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 24, 2026
  15. ambv commented on Feb 24, 2026

    @ambv
    ambv
    on Feb 24, 2026
    Contributor
    More actions

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

    Reacted by Bartosz Sławecki
  16. ambv
    closed this as completedon Feb 24, 2026
  17. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-143535: Dispatch on the second argument if generic method is…

    1d94bd7
  18. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-143535: Dispatch on the second argument if generic method is…

    50b2f3a
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 fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • docs issues
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.