A generic function taking a TypedDict type erroneously reports missing __required_keys__
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When calling a generic function taking a type variable bound to a typed dictionary type, MyPy reports that the typed dictionary type does not have __required_keys__ and __optional_keys__.
Pyright does not report any errors for the same code.
To Reproduce
from typing import Any, TypedDict, TypeVar, TypeGuard, Mapping
class _A(TypedDict):
i: int
class _B(TypedDict):
s: str
TD = TypeVar("TD", _A,_B)
def is_ab(td: type[TD], doc: Mapping[str, Any]) -> bool:
for k in doc.keys():
if k not in td.__required_keys__ and k not in td.__optional_keys__:
return False
for k in td.__required_keys__:
if k not in doc:
return False
return True
def is_a(doc: Mapping[str, Any]) -> TypeGuard[_A]:
return is_ab(_A, doc)
def is_b(doc: Mapping[str, Any]) -> TypeGuard[_B]:
return is_ab(_B, doc)
xa: Mapping[str, Any] = {"i": 1}
xb: Mapping[str, Any] = {"s": "B"}
if is_a(xa):
a: _A = xa
if is_b(xb):
b: _B = xb
Expected Behavior
No errors should be reported because TD is bound to either _A or _B, which have all special TypedDict attributes.
Actual Behavior
When running MyPy against the code above, reports these errors:
mypy --strict td.py
td.py:13: error: "type[_A]" has no attribute "__required_keys__" [attr-defined]
td.py:13: error: "type[_B]" has no attribute "__required_keys__" [attr-defined]
td.py:13: error: "type[_A]" has no attribute "__optional_keys__" [attr-defined]
td.py:13: error: "type[_B]" has no attribute "__optional_keys__" [attr-defined]
td.py:16: error: "type[_A]" has no attribute "__required_keys__" [attr-defined]
td.py:16: error: "type[_B]" has no attribute "__required_keys__" [attr-defined]
Found 6 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.10.0 on Windows
- Mypy command-line flags:
--strict(same behavior without) - Mypy configuration options from
mypy.ini(and other config files): not used - Python version used: 3.10
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.
Direzione di ricerca
Inizia con il riproduttore td.py e la gestione delle funzioni generiche le cui variabili di tipo comprendono tipi TypedDict. Conferma gli errori attr-defined attuali con --strict, quindi aggiungi un test di regressione che mostri che required_keys e optional_keys sono accettati per _A e _B e che il riproduttore non segnala errori.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 45/100