python / python/mypy

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

Aperta
#21,925 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-reachability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi di repro.py e confronta mypy repro.py con la compilazione di Python 3.13, includendo i capture con guardia e gli or-pattern annidati. Segui la validazione esistente degli or-pattern e aggiungi il controllo mancante per i pattern irraggiungibili; il lavoro è completo quando mypy segnala i pattern irrefutabili non finali non validi, accettando i casi validi con guardia o finali.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.