Skip to content

TypeVar in higher order decorator with type alias not resolved properly #18503

Open
@cdce8p

Description

@cdce8p

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions