Skip to content

Pattern matching is not exhaustive in a weird edge case with an enum member subject #18875

Closed as duplicate of#18440
@injust

Description

@injust

Bug Report

  1. Access an enum member by name, but the name cannot be inline (e.g. you can access it from a variable)
  2. Directly use the enum member as the subject in a match statement

If you do this, mypy does not think the match statement is exhaustive.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=0d11136907ace743b0f2412ad0f26bb1

from enum import Enum, auto
from typing import reveal_type


class Thing(Enum):
    ONE = auto()
    TWO = auto()


two = "TWO"


def good() -> int:
    version = Thing[two]
    match version:
        case Thing.ONE:
            return 1
        case Thing.TWO:
            return 2


def bad() -> int:
    match Thing[two]:
        case Thing.ONE:
            return 1
        case Thing.TWO:
            return 2
        case _ as thing:
            reveal_type(thing)

If you replace Thing[two] with Thing["TWO"], the problem also goes away.

Expected Behavior

Both match statements are exhaustive, so there is a return statement for all cases.

Actual Behavior

main.py:22: error: Missing return statement  [return]
main.py:29: note: Revealed type is "__main__.Thing"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: --strict
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions