diff --git a/stubs/cachetools/@tests/test_cases/check_cachetools_wrapped.py b/stubs/cachetools/@tests/test_cases/check_cachetools_wrapped.py new file mode 100644 index 000000000000..4f97376a8219 --- /dev/null +++ b/stubs/cachetools/@tests/test_cases/check_cachetools_wrapped.py @@ -0,0 +1,14 @@ +from typing import Any + +from cachetools import LRUCache, cached + +cache: LRUCache[str, Any] = LRUCache(maxsize=128) + + +@cached(cache) +def example_function(x: int) -> str: + return str(x) + + +original_function = example_function.__wrapped__ +result = original_function(42) diff --git a/stubs/cachetools/cachetools/__init__.pyi b/stubs/cachetools/cachetools/__init__.pyi index a7c0906e0e2a..1956d2ad29db 100644 --- a/stubs/cachetools/cachetools/__init__.pyi +++ b/stubs/cachetools/cachetools/__init__.pyi @@ -1,8 +1,9 @@ -from _typeshed import IdentityFunction, Unused +from _typeshed import Unused from collections.abc import Callable, Iterator, MutableMapping, Sequence from contextlib import AbstractContextManager +from functools import _Wrapped from typing import Any, TypeVar, overload -from typing_extensions import deprecated +from typing_extensions import ParamSpec, deprecated __all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "MRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") __version__: str @@ -10,6 +11,8 @@ __version__: str _KT = TypeVar("_KT") _VT = TypeVar("_VT") _T = TypeVar("_T") +_R = TypeVar("_R") +_P = ParamSpec("_P") class Cache(MutableMapping[_KT, _VT]): @overload @@ -104,9 +107,9 @@ def cached( key: Callable[..., _KT] = ..., lock: AbstractContextManager[Any] | None = None, info: bool = False, -) -> IdentityFunction: ... +) -> Callable[[Callable[_P, _R]], _Wrapped[_P, _R, _P, _R]]: ... def cachedmethod( cache: Callable[[Any], MutableMapping[_KT, Any] | None], key: Callable[..., _KT] = ..., lock: Callable[[Any], AbstractContextManager[Any]] | None = None, -) -> IdentityFunction: ... +) -> Callable[[Callable[_P, _R]], _Wrapped[_P, _R, _P, _R]]: ... diff --git a/stubs/cachetools/cachetools/func.pyi b/stubs/cachetools/cachetools/func.pyi index 070383e58e27..011c1ec831fb 100644 --- a/stubs/cachetools/cachetools/func.pyi +++ b/stubs/cachetools/cachetools/func.pyi @@ -1,19 +1,24 @@ -from _typeshed import IdentityFunction from collections.abc import Callable, Sequence -from typing import TypeVar +from typing import TypeVar, overload from typing_extensions import deprecated +from . import _Cached + __all__ = ("fifo_cache", "lfu_cache", "lru_cache", "mru_cache", "rr_cache", "ttl_cache") _T = TypeVar("_T") +_S = TypeVar("_S") -def fifo_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... -def lfu_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... -def lru_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... +def fifo_cache(maxsize: float | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... +def lfu_cache(maxsize: float | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... +def lru_cache(maxsize: float | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... @deprecated("@mru_cache is deprecated") -def mru_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ... +def mru_cache(maxsize: float | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... +@overload +def rr_cache(maxsize: float | None = 128, *, typed: bool = False) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... +@overload def rr_cache( - maxsize: float | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False -) -> IdentityFunction: ... + maxsize: float | None, choice: Callable[[Sequence[_S]], _S], typed: bool = False +) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... def ttl_cache( maxsize: float | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False -) -> IdentityFunction: ... +) -> Callable[[Callable[..., _T]], _Cached[_T]]: ...