mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Decorator factory with generic is not properly bound

Open bersbersbers opened this issue 2 years ago • 0 comments

Bug Report

I hope I got the title approximately right. In the example below, mypy does not seem to properly bind the return type of the decorated function (int) to T.

To Reproduce

from typing import Callable, ParamSpec, TypeVar

T = TypeVar("T")
P = ParamSpec("P")

def decorator_factory(
    _decorator: Callable[[Callable[P, T]], T]
) -> Callable[[Callable[P, T]], Callable[P, T]]:
    ...

@decorator_factory
def decorator(func: Callable[P, T]) -> T:
    return func()

@decorator
def fun() -> int:
    return 0

var = fun()

reveal_type(decorator_factory)
reveal_type(decorator)
reveal_type(fun)
reveal_type(var)  # expected 'int', got 'T`-2' (Pylance reports 'int')

Expected Behavior

  • int

Actual Behavior

  • T`-2

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: none
  • Python version used: 3.11.4

bersbersbers avatar Jul 04 '23 22:07 bersbersbers