Can't infer T where Optional[T] is Optional[MyUnion]
Offen
Dieses Issue hat noch niemand übernommen.
bug
false-positive
priority-1-normal
topic-type-variables
topic-union-types
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
- Are you reporting a bug, or opening a feature request?
Bug
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
from typing import Optional, Union, TypeVar
MyUnion = Union[int, str]
T = TypeVar("T")
def not_none(x: Optional[T]) -> T:
assert x is not None
return x
# Works
def f(i: Optional[int]) -> int:
return not_none(i)
# Doesn't work
def g(u: Optional[MyUnion]) -> MyUnion:
return not_none(u)
- What is the actual behavior/output?
a.py:19: error: Incompatible return value type (got "object", expected "Union[int, str]")
- What is the behavior/output you expect?
No error.
- What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
mypy --version is 0.710+dev.70a4c9858fb3e736dbf4bc6cc9243c5d49bf2e5a
py --version is Python 3.7.2
Still see the issue after installing from master.
- What are the mypy flags you are using? (For example --strict-optional)
None
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, mypy für den minimalen Repro in a.py auszuführen, und bestätige, dass f() erfolgreich ist, während g() object statt Union[int, str] meldet. Verfolge die Typinferenz für not_none(Optional[MyUnion]); abgeschlossen ist die Aufgabe, wenn der Repro keinen Fehler erzeugt und dabei den erwarteten Rückgabetyp beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100