Skip to content

[2.2 regression] unreachable code regression with custom equality enum membership check #21703

Description

@blbelew

Bug Report

Mypy 2.2.0 introduced a regression from 2.1.0 with equality comparisons with custom integer enums.

Using the config option warn_unreachable = True, the following code checks successfully in mypy 2.1.0, but under mypy 2.2.0 fails with

test.py:23: error: Statement is unreachable [unreachable]

despite the code actually hitting this path at runtime and printing "Success".

To Reproduce

#!/usr/bin/env python3importenumimportsysimporttypingclassE(enum.Enum):
foo=1bar=2def__int__(self) ->int:
returnint(self.value)
def__eq__(self, other: typing.Any) ->bool:
returnbool(int(self) ==other)
classTest:
def__init__(self) ->None:
self.val: int=1t=Test()
ift.valin [E.foo, E.bar]:
print("success")
sys.exit(0)
print("fail")
sys.exit(1)

Your Environment

  • Mypy version used: 2.2.0
  • Mypy command-line flags: mypy test.py
  • Mypy configuration options from mypy.ini (and other config files): warn_unreachable = True
  • Python version used: 3.11

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