Skip to content

Enum members are Enum themselves. Yet mypy sometimes gets confused #9817

@udifuchs

Description

@udifuchs

Enum members are Enum themselves. Yet mypy sometimes gets confused.
For example:

from enum import Enum

class Spam(Enum):
    FOO = "Foo"

m = Spam.FOO
print(m.FOO.FOO)  # Valid python. mypy error: "str" has no attribute "FOO"
print(m == m.FOO)  # Valid python. mypy error: Non-overlapping equality check
print(m.FOO.startswith("Foo"))  # Invalid python. mypy does not complain.

The first two print calls are valid python code. mypy thinks that m.FOO is str. (mypy only shows an error on the second print if --strict-equality is specified.)
The last print raises an AttributeError. mypy does not complain because it things that m.FOO is str.

$ mypy --version
mypy 0.790

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions