python / python/mypy

Add support for TypedDict extra_items= and closed= (PEP 728)

Offen
#18,176 5 Kommentare 30 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Edit from maintainers: mypy supports closed TypedDicts since version 2.2


Feature

PEP 728 proposes changes to TypedDict, including the ability to specify the types of unspecified keys.

This has been implemented in typing_extensions with the __extra_items__ keyword since version 4.10.

I would like to see support for this added to mypy.

Pitch

I am working on using mypy to validate Python code associated with rest API services. It is not uncommon for the OpenAPI specs we use to include objects whose additionalProperties values map to schemas. This is how OpenAPI handles the case of objects whose keywords are not known in advance, but whose values must be of a particular type.

I want to be able to type annotate these schemas in Python, but currently there is no good mechanism for doing this. That is one of the problems that PEP 728 attempts to solve.

Currently with mypy I have found a few ways to handle this (in the absence of adding the requested support):

  1. As noted in the PEP, one can always suppress the typeddict-unknown-key errors. But of course that means no type checking is happening for the additional keys being used.
  2. I haven't coded it up yet, but I suspect with overloading I could probably come up with something that works for this. Basically, I could overload the getitem/setitem methods in a Protocol, specifying the correct return types for the various pre-defined keys (as Literals), and then add "generic" fallback get/set item methods for the "extra key type".
  3. It's possible I could come up with some Frankensteining of TypedDicts and defaultdicts that accomplishes this. Although that has the downside that the dict would always return a value regardless of the key, and that's not desirable.

For both 2 and 3, it would definitely be less clean and much lengthier than using the solution in typing_extensions.

From my research when trying to figure out how to handle this problem, I get the impression I am not the only one trying to handle this case. That is also indicated by the fact that the current JSON schema and OpenAPI spec includes this ability. And of course, that is part of the motivation for the PEP being opened in the first place.

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 dem Lesen von PEP 728 und der verlinkten typing_extensions-Dokumentation zu TypedDict.extra_items. Untersuche dann mypys bestehende TypedDict-Behandlung und Tests, um den erforderlichen Umfang für die Unterstützung von extra_items= und closed= zu bestimmen; abgeschlossen ist die Arbeit, wenn mypy diese Annotationen akzeptiert und korrekt prüft.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
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.