Type is lost after merge with `Union[Any, None]`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
from typing import Any
def func(d: dict[int, Any]) -> None:
x: int | None
x = d.get(1)
reveal_type(x) # typing is lost -> Union[Any, None]
d.get get's correctly inferred as Any | None. However, merging the existing type int | None with Any | None results in Any | None. I.e. mypy looses the original type information. That is unexpected.
Comparing it to a type merge with just Any:
def f(a: Any) -> None:
x: int | None
x = a
reveal_type(x) # int | None
In this case x retains the type information, as expected.
--
Another example which doesn't rely on dict.get
def g(a: Any | None) -> None:
x: int | None
x = a
reveal_type(x) # typing is lost -> Union[Any, None]
Expected Behavior
Mypy should retain the original type information even after merging it with Any | None.
Your Environment
- Mypy version used: 0.971
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 mit den drei Reproduktionsfunktionen im Issue und führe sie mit mypy und reveal_type aus, um das aktuelle Verhalten zu bestätigen. Verfolge die Logik zum Zusammenführen von Typen und zu Zuweisungen für einen vorhandenen Wert vom Typ int | None, der mit Any | None kombiniert wird. Als abgeschlossen gilt die Aufgabe, wenn der abgeleitete Typ in allen gezeigten Beispielen int | None bleibt und eine Regressionstestabdeckung für diese Fälle vorhanden ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100