mypy icon indicating copy to clipboard operation
mypy copied to clipboard

self: Ident[Self] wrongly rejected for an identity type alias (type Ident[T] = T).

Open Moussa-Mikhail opened this issue 1 month ago • 0 comments

Bug Report

Mypy reports "Method cannot have explicit self annotation and Self type [misc]" when self or cls are annotated with Ident[Self] where type Ident[T] = T. Since Ident[Self] = Self mypy should accept annotations of Ident[Self]. This also affects type Anno[T] = Annotated[T, '']. Pyright and ty don't report any errors in either case.

This issue is similar to #21917 and I explicitly noted it in #21928.

To Reproduce

from typing import Self, Annotated

type Ident[T] = T

type Anno[T] = Annotated[T, ""]

class C:
    def foo(self: Ident[Self], x: int) -> None:
        ...

    @classmethod
    def bar(cls: Ident[type[Self]], x: int) -> None:
        ...

    @classmethod
    def baz(cls: type[Ident[Self]], x: int) -> None:
        ...
        
    def anno(self: Anno[Self], x: int) -> None:
        ...

https://mypy-play.net/?gist=6a1c9a95a6ddd12ead7d71cffdf39ac6

Actual Behavior

mypy reports: main.py:8: error: Method cannot have explicit self annotation and Self type [misc] main.py:12: error: Method cannot have explicit self annotation and Self type [misc] main.py:16: error: Method cannot have explicit self annotation and Self type [misc] main.py:19: error: Method cannot have explicit self annotation and Self type [misc] Found 4 errors in 1 file (checked 1 source file)

My Environment

  • Mypy version used: current master as of 26/10/11, latest release 2.4.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12

Moussa-Mikhail avatar Oct 12 '26 00:10 Moussa-Mikhail