python / python/mypy

Irrefutable pattern before the last case in a match statement passes type checking but is a SyntaxError at runtime

Open
#21,925 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-reachability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy reports nothing for match statements that CPython rejects at compile time with SyntaxError: name capture 'y' makes remaining patterns unreachable. An unguarded capture or wildcard pattern in any case except the last one makes the remaining cases unreachable, so the compiler refuses the file, but mypy checks it clean. That means a file that cannot even be imported passes type checking.

The same applies to an irrefutable alternative in a non-final position of an or pattern, e.g. case _ | 1:, which CPython rejects wherever the or pattern appears, including nested inside sequence, mapping and class patterns.

To Reproduce

def f(x: int) -> str:
    match x:
        case y:
            return "capture"
        case _:
            return "wildcard"

Expected Behavior

An error on case y:, matching the runtime behavior:

$ python repro.py
  File "repro.py", line 3
    case y:
         ^
SyntaxError: name capture 'y' makes remaining patterns unreachable

For reference, a case is fine if it has a guard (case y if y > 0:), and an irrefutable pattern in the last case is fine. Inside an or pattern an irrefutable alternative is only allowed in the last position, regardless of guards or which case it is in.

Actual Behavior

$ mypy repro.py
Success: no issues found in 1 source file

mypy already implements the closely related compile time check for or patterns ("Alternative patterns bind different names"), so I think this one is missing rather than intentionally skipped. I have a fix ready and will put up a PR shortly.

Your Environment

  • Mypy version used: mypy 2.4.0+dev.3ad6157f9b66e71738bf39cae939aef7c3fd7ea8 (current master, also with --native-parser)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the repro.py examples and compare mypy repro.py with Python 3.13 compilation, including guarded captures and nested or patterns. Trace the existing or-pattern validation and add the missing unreachable-pattern check; done means mypy reports the invalid non-final irrefutable patterns while accepting valid guarded or final cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.