Flow typing doesn't infer generic arguments
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
I think the following should type check, but mypy errors on the last line. It's unable to infer that matching on the type of x.inner means we know that we have a Foo[str] and not a Foo[int].
#!/usr/bin/env python
from typing import *
T = TypeVar('T')
class Foo(Generic[T]):
def __init__(self, inner: T) -> None:
self.inner = inner
def foo(x: Foo[int] | Foo[str]) -> Foo[str]:
match x.inner:
case int():
raise Exception("Unsupported")
case str():
return x # error: Incompatible return value type (got "Foo[int] | Foo[str]", expected "Foo[str]") [return-value]
To Reproduce
Gist URL: https://gist.github.com/mypy-play/2be996e76f0d979d32be113f891803ed
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=2be996e76f0d979d32be113f891803ed
Expected Behavior
It should type check :)
Actual Behavior
error: Incompatible return value type (got "Foo[int] | Foo[str]", expected "Foo[str]") [return-value]
Your Environment
- Mypy version used: mypy 1.10.0 (compiled: yes)
- Mypy command-line flags: `--strict``
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.12
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
Führen Sie zunächst den bereitgestellten Python-3.12-Reproducer mit mypy 1.10.0 und --strict unter Verwendung des verlinkten Gists oder Playgrounds aus. Verfolgen Sie das Flow-Typing-Verhalten rund um das match auf x.inner und überprüfen Sie, dass das Beispiel Foo[str] ohne den Fehler wegen inkompatibler Rückgabe zurückgibt.
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
- 42/100