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
commented
Feb 8, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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"
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
johnslavik
commented
Feb 9, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
@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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
johnslavik
commented
Feb 16, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
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"?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ljfp
pushed a commit
to ljfp/cpython
that referenced
this pull request
Apr 25, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|