Skip to content

False-positive "Non-overlapping equality check" comparing Enums self to members #17317

@bersbersbers

Description

@bersbersbers

Bug Report

mypy does not understand that in an Enum, self can be compared to its members.

To Reproduce

from enum import Enum, auto

class Glass(Enum):
    HALF_FULL = auto()
    HALF_EMPTY = auto()

    def print(self) -> str:
        if self == self.HALF_FULL:
            print("half full")
            return

        raise ValueError("Glass is not half-full.")

# prints "half full"
Glass.HALF_FULL.print()

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=edf65b45f562ed246f05592cc02f1fda

Expected Behavior

No error.

Actual Behavior

main.py:9: error: Non-overlapping equality check (left operand type: "Glass", right operand type: "auto")  [comparison-overlap]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.3

#16327 seems related.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions