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

Broken reachability checks in match-case #13989

Closed
denballakh opened this issue Nov 2, 2022 · 2 comments
Closed

Broken reachability checks in match-case #13989

denballakh opened this issue Nov 2, 2022 · 2 comments
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder

Comments

@denballakh
Copy link
Contributor

Bug Report
I have two small similar examples for you:
1:

def f(x: object, y: object) -> int | None: # Missing return statement
    match x, y:
        case int(), int() if True:
            return 42
        case _:
            return None

2:

def f(x: object, y: object) -> int | None: # Missing return statement
    match x, y:
        case _, _ if True:
            return 42
        case _:
            return None

Expected Behavior

  1. No Missing return statement error, because code after match statement is unreachable.
  2. No Missing return statement error and error statement is unreachable on line case _:

Actual Behavior

  1. Error Missing return statement.
  2. Error Missing return statement. No statement is unreachable error on line case _:

Error Missing return statement will disappear if i remove if True: condition (in both cases).
Error statement is unreachable will appear if i replace case _, _: with case _:. Removing condition doesnt help.

Your Environment

  • Mypy version used: mypy 0.982 (compiled: no)
  • Mypy command-line flags: no
  • Mypy configuration options from mypy.ini: no
  • Python version used: 3.11
  • Windows 10

And now for something a bit different

def f(x: object, y: object) -> int | None:
    match x, y:
        case int(), int():
            return x + y # Unsupported left operand type for + ("object")
        case _:
            return None

Mypy cannot narrow types of tuple elements.

@denballakh denballakh added the bug mypy got something wrong label Nov 2, 2022
@AlexWaygood AlexWaygood added topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder labels Nov 2, 2022
@tmke8
Copy link
Contributor

tmke8 commented Nov 2, 2022

Regarding your "And now for something a bit different", there is an issue for that: #12364.

@erictraut
Copy link

This is a duplicate of #12534.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 14, 2023
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 topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

5 participants