Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
injust opened this issue Apr 3, 2025 · 2 comments
Labels
bug mypy got something wrong topic-enum topic-match-statement Python 3.10's match statement

Comments

@injust
Copy link

injust commented Apr 3, 2025

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
@injust injust added the bug mypy got something wrong label Apr 3, 2025
@injust injust changed the title Pattern matching is not exhaustive if enum member subject is accessed by name and the name is in a variable Pattern matching is not exhaustive in a weird edge case with an enum member subject Apr 3, 2025
@brianschubert brianschubert added topic-enum topic-match-statement Python 3.10's match statement labels Apr 3, 2025
@sterliakov
Copy link
Collaborator

Given that your good version works, is it the same as #18440? Currently we do not narrow types of complex expressions in match statement other than function calls.

@injust
Copy link
Author

injust commented Apr 4, 2025

Given that your good version works, is it the same as #18440? Currently we do not narrow types of complex expressions in match statement other than function calls.

Yes, it seems like the same issue. In the repro for #18440, if you change the function b from

match d[k]:

to

foo = d[k]
match foo:

Then it type checks. This is the same behaviour that I'm seeing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum topic-match-statement Python 3.10's match statement
Projects
None yet
Development

No branches or pull requests

3 participants