Skip to content
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

Untrue "missing return statement" after exhaustive match on enum #16276

Closed
aleksanb opened this issue Oct 17, 2023 · 6 comments
Closed

Untrue "missing return statement" after exhaustive match on enum #16276

aleksanb opened this issue Oct 17, 2023 · 6 comments
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement

Comments

@aleksanb
Copy link
Contributor

aleksanb commented Oct 17, 2023

Bug Report

Exhaustive match on enums fails if no intermediate alias of the match variable is given.

To Reproduce

from enum import Enum

class Choices(str, Enum):
    A="A"
    B="B"
    
    
def matcher_implicit(a: str) -> str:
    match Choices(a):
        case Choices.A:
            return 'ok'
        case Choices.B:
            return 'schmok'
            

def matcher_alias(a: str) -> str:
    explicit_alias = Choices(a)
    match explicit_alias:
        case Choices.A:
            return 'ok'
        case Choices.B:
            return 'schmok'

You can run this on https://mypy-play.net/?mypy=latest&python=3.11, and see that only matcher_implicit fails, but there's no reason why it should!

Expected Behavior

Both should typecheck correctly

Actual Behavior

The first one fails typechecking, with a fake "missing return statement" error emitted.

Your Environment

  • Mypy version used: 1.5.1 and 1.6.0 (fails on both).
  • sample fails on mypy-play.
@aleksanb aleksanb added the bug mypy got something wrong label Oct 17, 2023
@Sullynr2Wolf
Copy link

Hello! @aleksanb Can I try to fix the issue? I am currently doing my assignment for software engineering and am supposed to try solve an issue like this one. You can assign the issue to me and I will try my best to help.

@hauntsaninja hauntsaninja added the topic-match-statement Python 3.10's match statement label Oct 18, 2023
@hauntsaninja
Copy link
Collaborator

Yes, don't ask, just open a PR.

(mypy is doing the equivalent of if Choices(a) == Choices.A ... elif Choices(a) == Choices.B where narrowing would be unsound because there's no guarantee Choices will return the same thing)

@hauntsaninja
Copy link
Collaborator

Duplicate of #12998

@hauntsaninja hauntsaninja marked this as a duplicate of #12998 Nov 12, 2023
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2023
@BeefaloKing
Copy link

BeefaloKing commented Oct 8, 2024

I don't think this is a duplicate of #12998 (fixed in 1.10?). I still have this issue with mypy 1.11+. Can we get this reopened?

@hauntsaninja
Copy link
Collaborator

This was indeed fixed in 1.10. Try it yourself in the playground. If you have some other issue, please open a new issue :-)

@BeefaloKing
Copy link

Ah, I see. The narrowing only works if you match against (e.g.) Choices.A rather than the literal 'A'.

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-match-statement Python 3.10's match statement
Projects
None yet
Development

No branches or pull requests

4 participants