python / python/mypy

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

Offen
#21,517 1 Kommentar 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-match-statement topic-reachability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Reproduktion von main.py und der Analyse der Exhaustivitätsprüfung für Klassenmuster mit as-Captures. Verifizieren Sie, dass die Fälle A | None keinen Fehler erzeugen und dass der unerreichbare Zweig case B(): die Exhaustivitätsprüfung nicht verändert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
65/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.