Skip to content

Unable to use typing.Self with Enums #18345

Open
@youtux

Description

@youtux

Bug Report

I expect to be able to use typing.Self for classmethods of an Enum subclass, but mypy fails the type check for such methods.

To Reproduce
https://mypy-play.net/?mypy=master&python=3.12&gist=9f529a65422ee83aba0312c91e74a935

from enum import *
from typing import *

class Foo(Enum):
    BAR = "bar"

    @classmethod
    def get_bar(cls) -> Self:
        return cls.BAR

Expected Behavior
Type checking should pass.

Actual Behavior
I get the following error:

main.py:9: error: Incompatible return value type (got "Foo", expected "Self")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.13.0, 1.14.0, master branch
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12, 3.13

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions