TypeVar in higher order decorator with type alias not resolved properly
Offen
Dieses Issue hat noch niemand übernommen.
bug
topic-type-alias
topic-type-variables
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
from collections.abc import Callable
type A1[**P, R] = Callable[[Callable[P, R]], Callable[P, R]]
type A2[**P, R] = Callable[P, R]
def decorator[**P, R]() -> A1[P, R]:
def inner(func: Callable[P, R]) -> Callable[P, R]:
return func
return inner
def decorator_working[**P, R]() -> Callable[[A2[P, R]], A2[P, R]]:
def inner(func: Callable[P, R]) -> Callable[P, R]:
return func
return inner
@decorator() # error
def f1(x: str) -> int:
return 1
@decorator_working()
def f2(x: str) -> int:
return 1
reveal_type(f1)
reveal_type(f2)
Condensed version for debugging
# mypy: allow-empty-bodies
from collections.abc import Callable
type A1[**P, R] = Callable[[Callable[P, R]], Callable[P, R]]
type A2[**P, R] = Callable[P, R]
def decorator[**P, R]() -> A1[P, R]: ...
def decorator_working[**P, R]() -> Callable[[A2[P, R]], A2[P, R]]: ...
@decorator()
def f1(x: str) -> int: ...
@decorator_working()
def f2(x: str) -> int: ...
reveal_type(f1)
reveal_type(f2)
Expected Behavior
Mypy should be able to resolve the TypeVars for decorator just like it does for decorator_working.
Actual Behavior
error: Argument 1 has incompatible type "Callable[[str], int]"; expected "Callable[[VarArg(Never), KwArg(Never)], Never]" [arg-type]
note: Revealed type is "def (*Never, **Never) -> Never"
note: Revealed type is "def (x: builtins.str) -> builtins.int"
Your Environment
- Mypy version used:
mypy 1.15.0+dev.68cffa7afe03d2b663aced9a70254e58704857db (compiled: no) - Python version:
3.13
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, das verkürzte Beispiel im Issue mit mypy unter Python 3.13 zu reproduzieren, und vergleiche dann die Diagnosen und die aufgedeckten Typen für decorator und decorator_working. Verfolge den Inferenzpfad des Higher-Order-Dekorators und des Typ-Alias; fertig ist die Aufgabe, wenn decorator seine TypeVars wie decorator_working auflöst, f1 akzeptiert und ohne Fehler den erwarteten Callable-Typ aufdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100