python / python/mypy

Support for default types in TypedDict

Offen
#6,131 17 Kommentare 36 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature priority-2-low topic-typed-dict
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

This problem arose from trying to type the environ argument for a WSGI application. There are specific keys of the environ dict that have non-str types, and then all the other keys are HTTP_ variables (or server-defined variables, which can be ignored for this purpose) with type str (or Text, depending on the version of Python).

I would like to get stronger typing on environ than Dict[str, Any], but there isn't a way to do that with TypedDict currently because the set of possible keys is unbounded (the client can send any HTTP header).

Ideally, I'd like to be able to do something like:

EnvironDict = TypedDict("EnvironDict", {
    "wsgi.version": Tuple[int, int],
    "wsgi.input": IO[str],
# ...
}, default_type=str, total=False)

and have this mean that any key is allowed in the dictionary and all unknown keys get a value type of str.

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

Das Issue beschreibt, TypedDict um einen Standardwerttyp für ansonsten unbekannte Schlüssel zu erweitern, wobei das WSGI environ-Beispiel als Motivation dient. Beginne mit der Überprüfung der bestehenden TypedDict-Unterstützung von mypy und ihrer Tests; als erledigt gilt die Aufgabe, wenn die vorgeschlagene Syntax definiert ist, beliebige Schlüssel den Standardtyp erhalten, explizite Schlüssel ihre Typen behalten und dieses Verhalten durch Tests abgedeckt ist.

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
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

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