8000 Guard clause prevents `match` from seeing that all paths return · Issue #12534 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Guard clause prevents match from seeing that all paths return #12534

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
bkovitz opened this issue Apr 6, 2022 · 2 comments · Fixed by #15882
Closed

Guard clause prevents match from seeing that all paths return #12534

bkovitz opened this issue Apr 6, 2022 · 2 comments · Fixed by #15882
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code

Comments

@bkovitz
Copy link
bkovitz commented Apr 6, 2022

Bug Report

In a match statement in which all cases include a return statement, mypy 0.942 falsely reports "Missing return statement [return]" if at least one case includes a guard clause.

To Reproduce

def func(e) -> int:
    match e:
        case x if True:
            return x
        case _:
            return 0

Or even more simply:

def func(e) -> int:
    match e:
        case _ if True:
            return 0

Expected Behavior

This code should type-check.

Actual Behavior

$ mypy --python-version 3.10 --show-error-codes x5.py
x5.py:1: error: Missing return statement  [return]
Found 1 error in 1 file (checked 1 source file)

Notes

If we change the first case to something that does not include a guard clause, like this:

def func(e) -> int:
    match e:
        case (x, _):
            return x
        case _:
            return 0

then mypy returns no errors.

Environment

  • Mypy version used: 0.942
  • Mypy command-line flags: --python-version 3.10 --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.10.2
  • Operating system and version: Ubuntu 18.04
@hauntsaninja
Copy link
Collaborator
hauntsaninja commented Aug 16, 2023

Opened a PR that should fix this. Are any of the people here encountering this in open source projects? I could use some more match statement coverage in https://github.com/hauntsaninja/mypy_primer

@hauntsaninja
Copy link
Collaborator

I've merged the fix, but if your project is open source, please let me know!

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 topic-reachability Detecting unreachable code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0