Mypy cannot guess callable return type (`Tuple[Type[T], ...] -> T`)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
It looks like mypy cannot detect some types like following type.
To Reproduce
Playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=f83671bbbc02711ac90962ae786b418b
from typing import Union, Type, Tuple, TypeVar
T = TypeVar("T")
def some_types(type_: Union[Type[T], Tuple[Type[T], ...]]) -> T:
...
def int_str_float_fn(a: int | str | float) -> None:
...
a = some_types((int, str, float))
int_str_float_fn(a) # should't be an error
I got an error like
main.py:14: error: Argument 1 to "int_str_float_fn" has incompatible type "object"; expected "int | str | float" [arg-type]
Expected Behavior
No error here.
I think VSCode has a quite good guess here.
Mypy seems guessing a is object.
Actual Behavior
Your Environment
- Mypy version used: latest
- Mypy command-line flags: in playground
- Mypy configuration options from
mypy.ini(and other config files): in playground - 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
Beginne damit, die verknüpfte mypy-play-Reproduktion auszuführen, und untersuche, wie mypy den Rückgabetyp von some_types ableitet, wenn ein heterogenes Tupel von Klassen übergeben wird. Verfolge den Pfad der Callable- und TypeVar-Inferenz und füge anschließend einen Regressionstest für dieses Beispiel hinzu oder aktualisiere ihn; fertig ist es, wenn der inferierte Wert als int | str | float akzeptiert wird, ohne einen Argumenttypfehler zu verursachen.
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
- Klar beschrieben
- Anfängerfreundlichkeit
- 38/100