gh-127750: Fix annotations in singledispatchmethod signature tests#143571
Merged
serhiy-storchaka merged 2 commits intopython:mainfrom Jan 11, 2026
Merged
gh-127750: Fix annotations in singledispatchmethod signature tests#143571serhiy-storchaka merged 2 commits intopython:mainfrom
serhiy-storchaka merged 2 commits intopython:mainfrom
Conversation
… the right params See https://github.com/python/cpython/pull/130309/changes#r2663516538. These functions don't annotate parameters for values used in single-dispatching (`item`), they annotate parameters one later (`arg`). This being legal relies on a bug -- pythonGH-84644, which is that `singledispatch` doesn't verify the annotation is on the "first" parameter. I think these functions should look like ```diff @functools.singledispatchmethod - def func(self, item, arg: int) -> str: + def func(self, item: int, arg) -> str: return str(item) @func.register - def _(self, item, arg: bytes) -> str: + def _(self, item: bytes, arg) -> str: return str(item) ``` (and signature tests updated accordingly) In practice, it doesn't matter how you annotate `func` here, because it's a default callback. But what matters is that any function passed to `register()` annotates the target parameter (always first positional in `singledispatch` and always second positional in `singledispatchmethod` (unless staticmethod, then the first) etc.) for the value to single-dispatch on; not some other, unrelated parameter (or return type, as presented in pythonGH-84644). Let's have a class with the same registree signature as these from the test: ```py class A: @functools.singledispatchmethod def func(self, item, arg: int) -> str: return 'argint' @func.register def _(self, item, arg: bytes) -> str: return 'argbytes' a = A() print(a.func(b'', 1)) ``` For this code, the output would be `argbytes`, even though `arg` is an integer `1`.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Member
Author
|
@serhiy-storchaka, did I get it correctly? Thanks for the quick review :) |
|
Thanks @johnslavik for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jan 11, 2026
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter. (cherry picked from commit 620a5b9) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
|
GH-143707 is a backport of this pull request to the 3.14 branch. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jan 11, 2026
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter. (cherry picked from commit 620a5b9) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
|
GH-143708 is a backport of this pull request to the 3.13 branch. |
reidenong
pushed a commit
to reidenong/cpython
that referenced
this pull request
Jan 12, 2026
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter.
thunder-coding
pushed a commit
to thunder-coding/cpython
that referenced
this pull request
Feb 15, 2026
…sts (pythonGH-143571) These tests relied on a bug -- pythongh-84644, which is that singledispatch doesn't verify the annotation is on the "first" parameter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to gh-130309.
See https://github.com/python/cpython/pull/130309/changes#r2663516538.
Details
These functions don't annotate parameters for values used in single-dispatching (
item), they annotate parameters one later (arg).This being legal relies on a bug -- GH-84644, which is that
singledispatchdoesn't verify the annotation is on the "first" parameter.In practice, it doesn't matter how you annotate
funchere, because it's a default callback. But what matters is that any function passed toregister()annotates the target parameter (always first positional insingledispatchand always second positional insingledispatchmethod(unless staticmethod, then the first) etc.) for the value to single-dispatch on; not some other, unrelated parameter (or return type, as presented in GH-84644).Let's have a class with the same registree signature as these from the test:
For this code, the output would be
argbytes, even thoughargis an integer1.