python / python/mypy

TypedDict -> dict compatibility

Offen
#13,122 6 Kommentare 10 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Feature

Allow for TypedDicts (or a new type specialized for this purpose) to be compatible with dict when the receiver declares it is not going to mutate the dict.

Pitch

When using a TypedDict, it is common to eventually pass it to a function that takes an arbitrary dict or return it to the caller expecting the same. Currently this is rejected, and the reason makes sense. For example, this is correctly rejected:

class DefinitelyContainsStatus(TypedDict):
    status: str

def delete_status(d: dict):
    del d['status']

d: DefinitelyContainsStatus = {'status': 'ok'}
delete_status(d)
assert d['status'] == 'ok'

If it were permitted, a type checker wouldn't be able to know how the dict is going to be mutated, and then any other references that may exist would still expect it to conform to the TypedDict.

However, this presumably valid case is also rejected:

def foo() -> DefinitelyContainsStatus:
    return {'status': 'ok'}

f: dict[str, str] = foo()

# error: Incompatible types in assignment (expression has type "DefinitelyContainsStatus", variable has type "Dict[str, str]")  [assignment]

Here's a real-world example that has lead to the creation of this feature request. In Flask it is allowed to return a dict from a view function, which it then converts to JSON. This is particularly convenient in order to apply type checking to a JSON-based HTTP API. However, when the return type is declared to be some TypedDict, this is rejected:

class Example(TypedDict):
    status: str

@blueprint.route("/example")
def example() -> Example:
    return {"status": "ok"}

# error: Value of type variable "ft.RouteDecorator" of function cannot be "Callable[[str], Example]"  [type-var]

On the Flask side of things, they specifically require a real dict, so they cannot change the type to accept Mapping. It would be quite useful to be able to allow returning TypedDict from a function to a caller that accepts dict.

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

Es werden keine Dateien, Tests oder Einstiegspunkte genannt. Beginne damit, die Kompatibilitätsbeispiele für TypedDict und dict im Issue zu untersuchen, und verfolge anschließend die bestehenden Regeln des Typecheckers für Zuweisungs- und Callable-Kompatibilität. Als abgeschlossen gilt die Änderung, wenn sichere, nicht mutierende Verwendungen akzeptiert werden, während das Löschbeispiel weiterhin abgelehnt wird.

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.