python / python/mypy

mypy reports false-positive [exhaustive-match] error when there is an unreachable match

Aperta
#21,517 1 commento 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

exhaustive-match incorrectly reports a missing A case when using a capture pattern (case A() as a) together with an unreachable extra branch.

The following code reports:

error: Match statement has unhandled case for values of type "A"  [exhaustive-match]

even though all possible values of A | None are handled.

Interestingly, the error disappears if the unreachable case B(): branch is removed.

This seems related to exhaustiveness analysis for class patterns with as captures.

To Reproduce

# mypy: enable-error-code=exhaustive-match

class A: pass
class B: pass

x: A | None

match x:
    case None:
        pass
    case A() as a:
        pass
    case B():
        pass

Expected Behavior

No errors should be reported.

x has type A | None, and both None and A are handled exhaustively.

The case B(): branch is unreachable and should not affect exhaustiveness checking.

Removing the case B(): branch currently makes mypy report no errors:

match x:
    case None:
        pass
    case A() as a:
        pass

Actual Behavior

main.py:8: error: Match statement has unhandled case for values of type "A"  [exhaustive-match]
main.py:8: note: If match statement is intended to be non-exhaustive, add `case _: pass`
Found 1 error in 1 file (checked 1 source file)

Your Environment

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 la riproduzione in main.py e l'analisi della corrispondenza esaustiva per i pattern di classe con catture as. Verifica che i casi A | None non producano errori e che il ramo irraggiungibile case B(): non modifichi il controllo dell'esaustività.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
65/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.