Incorrect type check for nested unions of 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
For all types X and Y, if a type checks correctly as X it should also check correctly as Union[X, Y].
This is not the case for at least one specific combination of unions and dicts. See reproduction below.
To Reproduce
from typing import Dict, Union
A = Dict[str, Union[str, Dict[str, str]]]
B = Union[A, Dict[str, str]]
example1: A = {"x": {"y": "z"}}
example2: B = {"x": {"y": "z"}}
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=c72e66a8600324e7b1c1af24a0c533ca
Expected Behavior
example1 and example2 type check as valid.
Actual Behavior
example2 fails to type check with:
main.py:7: error: Incompatible types in assignment (expression has type "Dict[str, Dict[str, str]]", variable has type "Union[Dict[str, Union[str, Dict[str, str]]], Dict[str, str]]") [assignment]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Python 3.10 and 3.11.
Mypy 1.3.0
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
Beginne damit, die bereitgestellte Python-Reproduktion auszuführen und das Verhalten der beiden Zuweisungen im verlinkten mypy playground zu vergleichen. Verfolge den Pfad der Typprüfung für verschachtelte Unions, die dicts enthalten; abgeschlossen ist die Aufgabe, wenn sowohl example1 als auch example2 ohne den gemeldeten Zuweisungsfehler typgeprüft werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100