python / python/mypy

Cannot turn existing dictionary into TypedDict

Aperta
#8,890 4 commenti 40 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-typed-dict
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

I expected any of these forms to be allowed by MyPy, but they aren't:

# typed_dict_test.py
from typing import TypedDict

class D(TypedDict):
    a: str
    b: int


d_untyped = {"a": "foo", "b": 1}

d0 = D({"a": "foo", "b": 1})  # The only one that works
print(d0)

d1 = D(d_untyped)
print(d1)

d2 = D(**d_untyped)
print(d2)

d3 = D(dict(d_untyped))
print(d3)

d4 = D(dict(**d_untyped))
print(d4)
$ python typed_dict_test.py
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
$ python -V
Python 3.8.3
$ mypy typed_dict_test.py 
typed_dict_test.py:13: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d1 = D(d_untyped)
         ^
typed_dict_test.py:16: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d2 = D(**d_untyped)
         ^
typed_dict_test.py:19: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d3 = D(dict(d_untyped))
         ^
typed_dict_test.py:22: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d4 = D(dict(**d_untyped))
         ^
Found 4 errors in 1 file (checked 1 source file)
$ cat mypy.ini 
[mypy]
warn_redundant_casts = True
warn_unused_configs = True
pretty = True
show_error_codes = True

disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True
no_implicit_optional = True
warn_unused_ignores = True
warn_return_any = True
no_implicit_reexport = True

I checked that cast(D, ...) works, but I wonder if this is the right way to proceed, or a workaround.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo la diagnostica con l'esempio di TypedDict nell'issue ed esamina il controllo dei costruttori di TypedDict di mypy. Confronta le forme literal e dict(...) accettate con le forme che usano dizionari esistenti e vengono rifiutate; il lavoro è completato quando le conversioni supportate previste sono verificate dal type checking in modo coerente con il comportamento a runtime e coperte dai test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.