python / python/cpython

`singledispatchmethod.register` fails with `typing.Self` annotation

Open
#130,827 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-typing type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-130829
  • gh-143465

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at functools.singledispatchmethod.register and reproduce the minimal example from the report on Python 3.13. Check the existing linked work before proceeding. Done means a method using typing.Self can be registered without the invalid-annotation TypeError, with regression coverage for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.