Skip to content

Untrue unreachable statements with list pattern matching #16272

Closed
@JanczarKurek

Description

Bug Report
When trying to use pattern matching statements that contain list-type matches mypy emits bogus "unreachable" errors.

To Reproduce
The following code

    from typing import Any

    def list_len(l: Any):
        match l:
            case []:
                return 0
            case [_]:
                return 1
            case _:
                return "Big number"

generates error

pattern_matching_mypy.py:6: error: Statement is unreachable  [unreachable]

when run with option --warn-unreachable. Curiously it is enough to hint at a possibility of l being a list by doing

    from typing import Any

    def list_len(l: Any | list):
        match l:
            case []:
                return 0
            case [_]:
                return 1
            case _:
                return "Big number"

to make the error disappear.

Expected Behavior
Both snippets of code should emit no errors as it is clearly possible to match l if it is a list.

Actual Behavior
The following error is generated

pattern_matching_mypy.py:6: error: Statement is unreachable  [unreachable]
pattern_matching_mypy.py:8: error: Statement is unreachable  [unreachable]

My Environment

  • Mypy version used: 1.3.0, 1.4.0, 1.6.0
  • Mypy command-line flags: --warn-unreachable
  • Python version used: 3.10.13, 3.11.5, 3.12.0

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions