Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

singledispatchmethod.register fails with typing.Self annotation #130827

Open
bvolkmer opened this issue Mar 4, 2025 · 1 comment
Open

singledispatchmethod.register fails with typing.Self annotation #130827

bvolkmer opened this issue Mar 4, 2025 · 1 comment
Labels
stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@bvolkmer
Copy link

bvolkmer commented Mar 4, 2025

Bug report

Bug description:

When using singledispatchmethod, the register decorator function raises an exception if self is annotated with typing.Self:

TypeError: Invalid annotation for 'self'. typing.Self is not a class.

Minimal example:

from functools import singledispatchmethod
from typing import Self


class Foo:
    @singledispatchmethod
    def bar(self: Self, arg: int | str) -> int | str: ...

    @bar.register
    def _(self: Self, arg: int) -> int:
        return arg


foo = Foo()

print(foo.bar(1))

The workaround is to remove the typing.Self annotation for the _ function.

(Same for singledispatch but that should not be used in that case anyway)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

@bvolkmer bvolkmer added the type-bug An unexpected behavior, bug, or error label Mar 4, 2025
@ZeroIntensity ZeroIntensity added stdlib Python modules in the Lib dir topic-typing labels Mar 4, 2025
@ZeroIntensity
Copy link
Member

Presumably, we want to:

  • Disallow Self on singledispatch.
  • But allow it on singledispatchmethod.

That seems easy enough, but we'll probably need a new internal helper to hide any talking between singledispatch and singledispatchmethod. I'll submit a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants