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 — `singledispatchmethod.register` fails with `typing.Self` annotation · Issue #130827 · python/cpython

singledispatchmethod.register fails with typing.Self annotation #130827

New issue
Copy link
New issue
Copy link
Open
Open
singledispatchmethod.register fails with typing.Self annotation#130827
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@bvolkmer
bvolkmer
opened on Mar 4, 2025
Issue body actions

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-130827: Support typing.Self annotations in singledispatchmethod() #130829
  • gh-84644: Fix singledispatch annotation parsing #143465

Activity

  1. bvolkmer
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Mar 4, 2025
  2. ZeroIntensity
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-typing
    on Mar 4, 2025
  3. ZeroIntensity commented on Mar 4, 2025

    @ZeroIntensity
    ZeroIntensity
    on Mar 4, 2025
    Member
    More actions

    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.

  4. ZeroIntensity
    added a commit that references this issue on Mar 4, 2025

    pythongh-130827: Support typing.Self in singledispatchmethod()

    d466dc4
  5. ZeroIntensity
    added
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 2, 2025
  6. johnslavik
    added 2 commits that reference this issue on Jan 6, 2026

    Add pythonGH-130827 test

    3115fd7

    Merge pythonGH-130827 test into `test_method_type_ann_register`

    57965a9
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/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.