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 python3
import enum
import sys
import typing

class E(enum.Enum):
    foo = 1
    bar = 2

    def __int__(self) -> int:
        return int(self.value)

    def __eq__(self, other: typing.Any) -> bool:
        return bool(int(self) == other)

class Test:
    def __init__(self) -> None:
        self.val: int  = 1

t = Test()

if t.val in [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

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions