python / python/mypy

Strange behavior using Tagged Union

Offen
#10,659 6 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

When using tagged unions, mypy infers correctly with with instance["tag"],
but fails with instance.get("tag")

To Reproduce

  1. Write the following code into tagged_union.py:
from typing import Literal, TypedDict, Union


class TaggedTypeA(TypedDict):
    tag: Literal["A"]
    is_a: bool


class TaggedTypeB(TypedDict):
    tag: Literal["B"]


TaggedUnion = Union[TaggedTypeA, TaggedTypeB]

instance_a: TaggedUnion = {"tag": "A", "is_a": True}

if instance_a["tag"] == "A":
    print(f"Am I type a? {instance_a['is_a']}")

if instance_a.get("tag") == "A":
    print(f"Am I type a this time? {instance_a['is_a']}")

  1. Run mypy tagged_union.py
  2. Get only one error

tagged_union.py:21: error: TypedDict "TaggedTypeB" has no key "is_a"

Error occurs in second if statement

Expected Behavior

I can see that mypy correctly infers the type of instance_a when I use instance_a["tag"] == "A" (the first if expression).

Therefore, I would expect the same behavior when using instance_a.get("tag") == "A" (the second if clause).

Actual Behavior
mypy correctly infers the type of instance_a when I use instance_a["tag"] == "A" (the first if expression).

mypy fails to infer the type when I use instance_a.get("tag") == "A".

In fact, mypy doesn't only not infer the correct type, it infers the opposite type TaggedTypeB which does not make sense at all

Your Environment

  • Mypy version used: 0.902
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.10
  • Operating system and version: Ubuntu 21.04

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 mit der tagged_union.py-Reproduktion und führe mypy tagged_union.py aus, um die Eingrenzung für den Zugriff über eckige Klammern und get() zu vergleichen. Verfolge den für TypedDict-Union-Typen verantwortlichen Pfad der Type Narrowing-Logik und füge anschließend einen Regressionstest hinzu oder aktualisiere ihn, der zeigt, dass beide Formen auf TaggedTypeA eingegrenzt werden und die relevante mypy-Testsuite bestehen.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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