`singledispatchmethod.register` fails with `typing.Self` annotation
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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