Exhaustiveness checking fails on non-trivial use of pattern matching
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Code using assert_never() to ensure exhaustive use of match-case yields a type error on many non-trivial cases.
mypy can only verify exhaustiveness when matching directly against a Union type or Enum value.
Though, proving the exhaustiveness of non-trivial matches may be beyond mypy domain. Here are a couple of examples for the sake of argument:
Matching on both [] and [x, *xs] on Sequence will not narrow the type
from collections.abc import Sequence
from typing import assert_never
def my_sum(s: Sequence[float]) -> float:
match s:
case []:
return 0
case [num, *rest]:
return num + my_sum(rest)
case _ as u:
# error: Argument 1 to "assert_never" has incompatible type "Sequence[float]"; expected "NoReturn"
return assert_never(u)
Matching against all Enum values that a single-member wrapper class may contain
import dataclasses, enum
from typing import assert_never, final
class E(enum.Enum):
A = enum.auto()
B = enum.auto()
@final
@dataclasses.dataclass(frozen=True)
class Foo:
x: E
def match_enum_attribute(f: Foo) -> None:
match f:
case Foo(E.A):
pass
case Foo(E.B):
pass
case _ as r:
# error: Argument 1 to "assert_never" has incompatible type "Foo"; expected "NoReturn" [arg-type]
assert_never(r)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die beiden Beispiele mit assert_never(), match-case, Sequence-Mustern und dem im Issue gezeigten umschlossenen Enum-Attribut zu reproduzieren. Verfolge mypy's Verhalten bei Exhaustiveness und Narrowing für diese Fälle; abgeschlossen bedeutet, dass unterstützte nicht-triviale Matches als vollständig erkannt werden, ohne die gemeldeten Typfehler, mit Abdeckung für die Beispiele.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100