incorrect inferred type accessing typed dict with static 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
mypy does not infer the correct type of a dictionary when constructing a subset of a TypedDict using a constant subset of keys.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=82fc1eb24628e1ba2aed8294b81e2912
from typing import TypedDict, reveal_type
class MyDict(TypedDict):
key_0: bool
key_1: int
key_2: bool
limits: MyDict = {
'key_0': True,
'key_1': 1,
'key_2': False
}
def test_func() -> dict[str, bool]:
result = {
label: limits.get(label, False)
for label in ('key_0', 'key_2')
}
reveal_type(limits.get('key_0', False))
reveal_type(result)
return result
test_func()
Expected Behavior
In the example above, I expect mypy to accept the return type since both key_0 and key_2 are annotated as bool
Actual Behavior
mypy complains about the return type with
Incompatible return value type (got "dict[str, object]", expected "dict[str, bool]") [return-value]
After mentioning it in my team chat @LefterisJP noticed that this version works https://mypy-play.net/?mypy=latest&python=3.12&gist=63bece9a183493b170f6f55527fbd7d3
from typing import TypedDict, reveal_type, Literal
class MyDict(TypedDict):
key_0: bool
key_1: int
key_2: bool
limits: MyDict = {
'key_0': True,
'key_1': 1,
'key_2': False
}
def test_func() -> dict[str, bool]:
keys: tuple[Literal['key_0', 'key_2'], ...] = ('key_0', 'key_2')
result: dict[str, bool] = {
label: limits.get(label, False)
for label in keys
}
reveal_type(limits.get('key_0', False))
reveal_type(result)
return result
test_func()
In this case, because the keys are explicitly typed as Literals, mypy infers the correct type.
This suggests that mypy is not analyzing constant tuples deeply enough to recognize the accessed keys.
Your Environment
- Mypy version used: mypy 1.18.2 (compiled: yes)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): I can reproduce it in an environment without any extra configuration. - Python version used: 3.11.13
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 riproducendo il comportamento segnalato negli esempi mypy-play collegati e confronta i tipi inferiti per la tupla di costanti semplici e per la tupla tipizzata esplicitamente con Literal. Il lavoro è concluso quando mypy accetta il tipo restituito dict[str, bool] mostrato per il caso con chiave costante senza richiedere l’annotazione esplicita Literal.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100