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 — gh-143535: Dispatch on the second argument if generic method is instance-bindable by johnslavik · Pull Request #144615 · python/cpython

Conversation

@johnslavik

@johnslavik johnslavik commented Feb 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Unbound methods (i.e. functions) become bound methods when regularly accessed as instance attributes. From the class level, the contract of Python is that the unbound method can be called like a function (the first argument to the call will be used as the conventional self) and the call will execute directly (no bound method in between). Following the contract, a natural expectation is that when generic methods are called as class attributes, the second argument will be used for dispatching, not the first (the conventional self).

To support descriptors like classmethod (that bind to something else than the instance), arguments are shifted by one even if __get__ method returns a method that is bound to a different object than the original self (_singledispatchmethod_get._obj). If one wants to receive that self in an arbitrary bound method as a regular argument, the workaround is easy: return a partial(bound method) to hack the check.

This is a low risk bugfix. The patch is meant to fix the following program:

class C:
   @singledispatchmethod
   def generic(self, x: object):
       return "generic"

   @generic.register
   def special(self, x: int):
       return "special"

assert C.generic(C(), 1) == "generic"  # should be "special"
  • Issue: singledispatchmethod dispatches on first argument to instance-bindable method called from class level #143535

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@johnslavik
Dispatch on the second argument if generic method is instance-bindable
5479e0f
@johnslavik johnslavik added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Feb 8, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Feb 8, 2026
singledispatchmethod dispatches on first argument to instance-bindable method called from class level #143535
Closed
johnslavik added 10 commits February 9, 2026 01:09
@johnslavik
Only cut off first argument if method becomes bound
3455cd9
@johnslavik
Cutoff on function, not method!
1142577
@johnslavik
Cut first argument after the fact!
d05750f
@johnslavik
Fix missing import
b81977b
@johnslavik
Fix formatting
f6afaec
@johnslavik
Intentionally don't handle an edge case
9dac097
@johnslavik
Add tests
354721b
@johnslavik
Fix formatting
d80e5d1
@johnslavik
Add news entry
97f23cd
@johnslavik
Rewrite comments
12cd97a
@johnslavik
johnslavik marked this pull request as ready for review February 9, 2026 01:23
@johnslavik
johnslavik requested a review from rhettinger as a code owner February 9, 2026 01:23
@bedevere-app bedevere-app Bot added the awaiting review label Feb 9, 2026
johnslavik added 3 commits February 9, 2026 02:42
@johnslavik
Inline argument cutoff condition
1a24e43
@johnslavik
Empty commit to unstuck GitHub processing recent push
615c284
@johnslavik
Add correct GitHub references
ab7d78c
@johnslavik

johnslavik commented Feb 9, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@rhettinger, do you like this fix? Or do you like a documentation change more?
FWIW I don't recall this bug being reported in the past, but the fix looks cheap.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik
johnslavik requested a review from serhiy-storchaka February 9, 2026 19:29
@johnslavik
Make the implementation more debuggable
dd804d2
@johnslavik
johnslavik requested a review from jaraco February 13, 2026 10:57
jaraco
jaraco approved these changes Feb 15, 2026
View reviewed changes
Comment thread Lib/functools.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/functools.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_functools.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/functools.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Feb 15, 2026
@jaraco jaraco mentioned this pull request Feb 15, 2026
gh-143535: Document dispatch behavior of singledispatchmethod #143592
Closed
@johnslavik
Fix staticmethod bug
85c318d
@picnixz

picnixz commented Feb 16, 2026

Copy link
Copy Markdown
Member

I think this can be both considered a new feature and a bugfix. I would consider it a new feature though so to avoid surprises. In particular, I would write a What's New entry as well.

johnslavik and encukou reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

johnslavik and others added 5 commits February 16, 2026 10:00
@johnslavik @jaraco
Assign the boolean directly
21dcd02
Co-authored-by: Jason R. Coombs <[email protected]>
@johnslavik
Simplify method dispatch
4c1cfd7
@johnslavik
More meaningful variable names
e3538f5
@johnslavik
Reiterate on the code
0878b55
@johnslavik
Write a "What's New" entry
373deb0
@johnslavik
johnslavik requested a review from AA-Turner as a code owner February 16, 2026 12:47
@johnslavik
johnslavik requested a review from picnixz February 16, 2026 12:48
@johnslavik

johnslavik commented Feb 16, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I've written the news entry and moved the code around a bit. @picnixz does it look good?
I assume we don't backport if it's a feature and we add a "What's New"?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik johnslavik removed needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Feb 16, 2026
@picnixz

picnixz commented Feb 16, 2026

Copy link
Copy Markdown
Member

New features are indeed not backported. I am a bit busy this week so I do not know when I can look at it again (and hearing from Serhiy would be good)

johnslavik reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv
ambv merged commit 97181bb into python:main Feb 24, 2026
51 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 24, 2026
@johnslavik
johnslavik deleted the fix-bound-arg-handling-singledispatchmethod branch February 24, 2026 20:13
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
@johnslavik @jaraco @brijkapadia
pythongh-143535: Dispatch on the second argument if generic method is…
1d94bd7
… instance-bindable (pythonGH-144615)

Co-authored-by: Jason R. Coombs <[email protected]>
@johnslavik johnslavik mentioned this pull request Mar 1, 2026
gh-84644: Fix singledispatch annotation parsing #143465
Merged
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@johnslavik @jaraco @ljfp
pythongh-143535: Dispatch on the second argument if generic method is…
50b2f3a
… instance-bindable (pythonGH-144615)

Co-authored-by: Jason R. Coombs <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jaraco jaraco jaraco approved these changes
@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger is a code owner
@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@picnixz picnixz Awaiting requested review from picnixz

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@johnslavik @picnixz @jaraco @ambv
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.