#21997·mypy

Checking a match on an enum becomes quadratically slow after the variable was narrowed by an == check

Author: sunny-zuoCreated Sep 16, 2026Updated Sep 17, 2026
Labelsbugperformancetopic-enum

Bug Report

Type-checking a match on a large enum becomes very slow when the variable was first narrowed by an == check with an early exit:

python
from enum import StrEnum

class E(StrEnum):
    M0 = "m0"
    M1 = "m1"
    # ... 1,200 members

def f(x: E) -> None:
    if x == E.M0:
        return
    match x:
        case E.M1:
            pass
        # ... through case E.M7

Removing the x == E.M0 block makes this much faster (~1.5s vs 22s locally).

To Reproduce

Your Environment

  • Mypy version used: 2.3.1, 2.4.0+dev.4d195a2
  • Python version used: 3.12