python / python/mypy

incorrect inferred type accessing typed dict with static keys

Offen
#19,961 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-inference topic-literal-types topic-typed-dict
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das gemeldete Verhalten in den verlinkten mypy-play-Beispielen zu reproduzieren und die abgeleiteten Typen für das Tupel mit Konstanten und das explizit mit Literal typisierte Tupel zu vergleichen. Erledigt ist es, wenn mypy den angegebenen Rückgabetyp dict[str, bool] für den Fall mit konstantem Schlüssel akzeptiert, ohne die explizite Literal-Annotation zu verlangen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.