Skip to content

match statement on enum is considered non-exhaustive #20240

@injust

Description

@injust

Bug Report

mypy thinks that a match statement covering all values of an enum is not exhaustive, when inside a function that has a reference to a global variable beforehand.

For some reason, the reference to the global variable obj before the match statement makes a difference.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.14&enable-error-code=exhaustive-match&gist=c270d39ad5ca2fe7fe730ec7b5176212

from enum import Enum, auto


class Thing(Enum):
    ONE = auto()


def main(foo: Thing) -> None:
    print(obj)

    match foo:
        case Thing.ONE:
            pass


obj = object()

Expected Behavior

The match statement is exhaustive, so mypy should not error.

And if you add a case _: pass like the note suggests (https://mypy-play.net/?mypy=latest&python=3.14&enable-error-code=exhaustive-match&flags=warn-unreachable&gist=9b375e563c684c62a2f4b4f24f30a679), then mypy should warn that the wildcard case is unreachable.

Actual Behavior

main.py:11: error: Match statement has unhandled case for values of type "Any | Any"  [exhaustive-match]
main.py:11: note: If match statement is intended to be non-exhaustive, add `case _: pass`
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: --enable-error-code=exhaustive-match
  • Python version used: 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable codetopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions