python / python/typing

[match-case]: `TypedDict` interaction with mapping pattern

Offen
#2,185 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic: feature
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
302
Ø Merge
23 Std.
Gemergte PRs (30 T.)
8

Beschreibung

It seems match-cases mapping pattern is predestined to work well with TypedDict, but currently the spec is silent regarding TypedDict subtyping with respect to match case: https://typing.python.org/en/latest/spec/typeddict.html#subtyping-with-mapping

At the time of writing, not a single type checker (tested with mypy=1.19.1, pyright=1.1.408, ty=0.0.17, pyrefly=0.53.0 and zuban=0.6.0) can solve the following tests[^1], suggesting that some standardization / typing spec guidance may be useful.

from typing import TypedDict, assert_type, assert_never

class D(TypedDict):
    name: str
    value: int

def test_match_key(x: D | int) -> None:
    match x:
        case {"name": _}:
            # runtime equivalent to (isinstance(x, Mapping) and "name" in x)
            assert_type(x, D)
        case _:
            assert_type(x, int)

def test_match_key_and_value(x: D | int) -> None:
    match x:
        case {"name": str()}:
            # runtime equivalent to (isinstance(x, Mapping)
            # and "name" in x and isinstance(x["name"], str))
            assert_type(x, D)
        case _:
            assert_type(x, int)

def test_non_match(x: D | int) -> None:
    match x:
        case {"value": str()}:
            # runtime equivalent to (isinstance(x, Mapping)
            # and "value" in x and isinstance(x["value"], str))
            assert_never(x)
        case _:
            assert_type(x, D | int)

[^1]: technically, it is possible to make a shared subclass of int and collections.abc.Mapping, so the assert_type in the case {...} are debatable, though the inference in the case _ branches should not be affected.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

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 dem Abschnitt zu TypedDict subtyping-with-mapping in der typing specification und den Regeln für mapping patterns in PEP 634. Führe die drei Beispiele mit den aufgeführten Type Checkern aus, um das aktuelle Verhalten zu vergleichen. Als abgeschlossen gilt die Aufgabe, wenn Einigkeit über die beabsichtigte Eingrenzung erreicht und standardisierte Anleitung mit den entsprechenden Validierungsfällen dokumentiert wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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