Invalid type inference for generic typed dicts
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
When we try to assert the type of an object obtained from get with default value from generic object bound to typed dict, then mypy shows next error:
error: Incompatible types in assignment (expression has type "object", variable has type "int") [assignment]
To Reproduce
from typing import NotRequired, TypedDict, TypeVar
class A(TypedDict):
i: NotRequired[int]
ParamsT = TypeVar('ParamsT', bound=A)
def foo(params: ParamsT) -> None:
i: int = params.get('i', 0)
foo(A())
Gist: https://gist.github.com/mypy-play/effc3f0d297540be8ca1bad7b08c5902
Play: https://mypy-play.net/?mypy=latest&python=3.12&gist=effc3f0d297540be8ca1bad7b08c5902
Expected Behavior
It is expected that result of expression params.get('i', 0) would be of type int since typed dict attribute is annotated as <int> and the default value type is <int> as well.
Actual Behavior
Mypy reports that result of expression params.get('i', 0) has type <object>.
Your Environment
- Mypy version used: mypy 1.15.0 (compiled: yes)
- Python version used: 3.13
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es wird keine Repository-Datei oder kein Testpfad genannt; beginne damit, die bereitgestellte Python-Reproduktion mit mypy 1.15.0 auszuführen und den abgeleiteten Typ für params.get('i', 0) zu untersuchen. Als erledigt gilt die Aufgabe, wenn dieser Ausdruck als int statt als object abgeleitet wird und eine Regressionstestabdeckung für den generischen TypedDict-Fall vorhanden ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100