See this discussion for details.
from typing import Callable, Generic, ParamSpec, TypeVar
_P = ParamSpec("_P")
_T = TypeVar("_T")
class _lru_cache_wrapper(Generic[_P, _T]):
def __call__(*args: _P.args, **kwargs: _P.kwargs) -> _T:
...
def cache(__user_function: Callable[_P, _T]) -> _lru_cache_wrapper[_P, _T]:
...
class Foo:
def not_cached(self) -> int:
return 0
@cache
def cached(self, x: str, y: int) -> int:
return 0
foo = Foo()
foo.not_cached()
foo.cached("hello", 42)
# Missing argument [20]: Call `_lru_cache_wrapper.__call__` expects argument `x`.
foo.cached()
# Incompatible parameter type [6]: In call `_lru_cache_wrapper.__call__`, for 1st positional only parameter expected `str` but got `int`.
foo.cached(1, 2)
See this discussion for details.