TypeVar in higher order decorator with type alias not resolved properly
Open
Nobody has claimed this yet.
bug
topic-type-alias
topic-type-variables
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the condensed example in the issue with mypy on Python 3.13, then compare the diagnostics and revealed types for decorator and decorator_working. Trace the higher-order decorator and type-alias inference path; done means decorator resolves its TypeVars like decorator_working, accepts f1, and reveals the expected callable type without an error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100