Skip to content

singledispatchmethod.register fails with typing.Self annotation #130827

Description

@bvolkmer

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions