Skip to content

Broken reachability checks in match-case #13989

Closed as not planned
Closed as not planned
@denballakh

Description

@denballakh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions