Cannot turn existing dictionary into TypedDict
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
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.
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.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
area/auth bug comp/agent P3 platform/discord type/security
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
NousResearch/hermes-agent#117848 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
bancolombia/sentinel#23 ·
-
test md ApertaCI
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100