Skip to content

Enum items are mistyped #11368

@ltworf

Description

@ltworf

Bug Report

The elements of an enum are mistyped to their value by mypy if used within the same class.

from enum import Enum
class Letter(Enum):
    A = 'A'
    B = 'B'

    @property
    def shout(self) -> str:
        s = {
            self.A: 'Aaaaah',
            self.B: 'Biiiih',
        }
        return s[self]

print(Letter.A.shout)
print(Letter.B.shout)

Using reveal_type shows that self.A is counted as a str, while it isn't.

Expected Behavior

I guess a special handling for Enum subclasses that understands what's going on and types self.A in a method of the class with the same type that Letter.A has outside of the class.

Your Environment

  • Mypy version used: 0.910
  • Python version used: 3.9
  • Operating system and version: debian sid

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions