python / python/mypy

Support exhaustiveness checking of single enumerable properties

Offen
#14,261 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-reachability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature

Support exhaustiveness checking of single boolean and enum properties and attributes of objects.

Pitch

The proposal is to improve exhaustiveness checking by supporting properties/attributes on a matched object. While it would be amazing to have infinitely nested support for this, I think we can recognize that there is value in supporting just single-level enumerable attributes.

In this example mypy isn't currently recognizing that all possible variations of Foo are handled, and gives an error on the assert_never().

from typing import assert_never

class Sentinel: ...

class Foo:
    prop: bool

val: Foo | Sentinel

match val:
    case Sentinel():
        ...
    case Foo(prop=True):
        ...
    case Foo(prop=False):
        ...
    case no_match:
        assert_never(no_match)  # Currently a false positive here, no_match should be <nothing>.

https://mypy-play.net/?mypy=latest&python=3.11&gist=1edcb8f3ea56334662ed2036404563a1

Likewise, if the feature is accepted, we should expect an unreachable error for the third case in this example.

from typing import assert_never

class Foo:
    prop: bool

val: Foo

match val:
    case Foo(prop=True):
        ...
    case Foo(prop=False):
        ...
    case Foo():
        ...  # Currently a false negative here, should be unreachable.

https://mypy-play.net/?mypy=latest&python=3.11&gist=74b23b6868c0c52056be1ade612b3052

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

Beginne mit den beiden Python-match-Beispielen und den darin verlinkten mypy-play-Reproduktionen. Lege das Verhalten hinsichtlich Vollständigkeit und unerreichbarer Fälle für boolesche Eigenschaften und Eigenschaften vom Typ Enum auf einer Ebene fest, und überprüfe anschließend, dass die im Issue beschriebenen False-Positive- und False-Negative-Ergebnisse korrigiert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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