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-127750: Fix annotations in singledispatchmethod signature tests by johnslavik · Pull Request #143571 · python/cpython

Conversation

@johnslavik

@johnslavik johnslavik commented Jan 8, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

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 singledispatch doesn't verify the annotation is on the "first" parameter.

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

Let's have a class with the same registree signature as these from the test:

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.

  • Issue: Regression in Django with singledispatchmethod on models #127750

Sorry, something went wrong.

Uh oh!

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

All reactions
@johnslavik
Fix TestSingleDispatch.test_method_signatures to put annotations on…
30a5245
… 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`.
@johnslavik
johnslavik requested a review from serhiy-storchaka January 8, 2026 15:12
@johnslavik
johnslavik requested a review from rhettinger as a code owner January 8, 2026 15:12
@johnslavik johnslavik added tests Tests in the Lib/test dir skip news needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Jan 8, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jan 8, 2026
Regression in Django with singledispatchmethod on models #127750
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Jan 8, 2026
serhiy-storchaka
serhiy-storchaka reviewed Jan 8, 2026
View reviewed changes
Comment thread Lib/test/test_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/test/test_functools.py Resolved

Uh oh!

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

@johnslavik @serhiy-storchaka
Keep tests analogous to test_signatures
85201c3
Co-authored-by: Serhiy Storchaka <[email protected]>
@johnslavik
johnslavik requested a review from serhiy-storchaka January 11, 2026 16:23
@johnslavik

johnslavik commented Jan 11, 2026

Copy link
Copy Markdown
Member Author

@serhiy-storchaka, did I get it correctly? Thanks for the quick review :)

All reactions

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka
serhiy-storchaka approved these changes Jan 11, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jan 11, 2026
@serhiy-storchaka
serhiy-storchaka merged commit 620a5b9 into python:main Jan 11, 2026
48 checks passed

Uh oh!

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

@miss-islington-app

miss-islington-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

Thanks @johnslavik for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Jan 11, 2026
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 11, 2026
@johnslavik @miss-islington
pythongh-127750: Fix annotations in singledispatchmethod signature te…
9c7f1fc
…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 <[email protected]>
@bedevere-app

bedevere-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

GH-143707 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 11, 2026
@johnslavik @miss-islington
pythongh-127750: Fix annotations in singledispatchmethod signature te…
c9eccc7
…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 <[email protected]>
@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jan 11, 2026
@bedevere-app

bedevere-app Bot commented Jan 11, 2026

Copy link
Copy Markdown

GH-143708 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Jan 11, 2026
serhiy-storchaka pushed a commit that referenced this pull request Jan 11, 2026
@miss-islington @johnslavik
[3.13] gh-127750: Fix annotations in singledispatchmethod signature t…
ea1ecf6
…ests (GH-143571) (GH-143708)

These tests relied on a bug -- gh-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 <[email protected]>
serhiy-storchaka pushed a commit that referenced this pull request Jan 11, 2026
@miss-islington @johnslavik
[3.14] gh-127750: Fix annotations in singledispatchmethod signature t…
ea26c88
…ests (GH-143571) (GH-143707)

These tests relied on a bug -- gh-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 <[email protected]>
reidenong pushed a commit to reidenong/cpython that referenced this pull request Jan 12, 2026
@johnslavik @reidenong
pythongh-127750: Fix annotations in singledispatchmethod signature te…
13c9f56
…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
@johnslavik @thunder-coding
pythongh-127750: Fix annotations in singledispatchmethod signature te…
5b0a063
…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.
@johnslavik
johnslavik deleted the fix-test-method-signatures branch July 21, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

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

Uh oh!

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

Assignees

No one assigned

Labels

skip news tests Tests in the Lib/test dir

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.

2 participants

@johnslavik @serhiy-storchaka
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.