TypeVar in higher order decorator with type alias not resolved properly
Abierto
Nadie ha tomado este issue todavía.
bug
topic-type-alias
topic-type-variables
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el ejemplo condensado del issue con mypy en Python 3.13 y compara después los diagnósticos y los tipos revelados para decorator y decorator_working. Sigue la ruta de inferencia del decorador de orden superior y del alias de tipo; se considera terminado cuando decorator resuelve sus TypeVars igual que decorator_working, acepta f1 y revela el tipo callable esperado sin errores.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100