Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cdce8p opened this issue Jan 21, 2025 · 0 comments
Open

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

cdce8p opened this issue Jan 21, 2025 · 0 comments
Labels
bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues topic-type-variables

Comments

@cdce8p
Copy link
Collaborator

cdce8p commented Jan 21, 2025

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
@cdce8p cdce8p added bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues topic-type-variables labels Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues topic-type-variables
Projects
None yet
Development

No branches or pull requests

1 participant