From bd49b3ab86e2e50c58180b47acdac549a1adcd3a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Mar 2025 07:37:27 +0000 Subject: [PATCH 1/9] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/cachetools/cachetools/func.pyi | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/stubs/cachetools/cachetools/func.pyi b/stubs/cachetools/cachetools/func.pyi index 3d51ba0251b3..011c1ec831fb 100644 --- a/stubs/cachetools/cachetools/func.pyi +++ b/stubs/cachetools/cachetools/func.pyi @@ -13,16 +13,12 @@ def lfu_cache(maxsize: float | None = 128, typed: bool = False) -> Callable[[Cal 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) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... - @overload -def rr_cache( - maxsize: float | None = 128, *, typed: bool = False -) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... +def rr_cache(maxsize: float | None = 128, *, typed: bool = False) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... @overload def rr_cache( 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 ) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... From 245b8b264e75c07de78788bb7ffa9c3096686f98 Mon Sep 17 00:00:00 2001 From: Mark Paronyan <markparonian@gmail.com> Date: Sat, 22 Mar 2025 10:16:01 +0000 Subject: [PATCH 2/9] Fix cachetools.__wrapped__ attribute typing for cached functions --- stubs/cachetools/cachetools/__init__.pyi | 13 ++++++++---- stubs/cachetools/cachetools/func.pyi | 27 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/stubs/cachetools/cachetools/__init__.pyi b/stubs/cachetools/cachetools/__init__.pyi index a7c0906e0e2a..1d7eeefb6366 100644 --- a/stubs/cachetools/cachetools/__init__.pyi +++ b/stubs/cachetools/cachetools/__init__.pyi @@ -1,7 +1,7 @@ -from _typeshed import IdentityFunction, Unused +from _typeshed import Unused from collections.abc import Callable, Iterator, MutableMapping, Sequence from contextlib import AbstractContextManager -from typing import Any, TypeVar, overload +from typing import Any, Protocol, TypeVar, overload from typing_extensions import deprecated __all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "MRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") @@ -10,6 +10,7 @@ __version__: str _KT = TypeVar("_KT") _VT = TypeVar("_VT") _T = TypeVar("_T") +_R = TypeVar("_R") class Cache(MutableMapping[_KT, _VT]): @overload @@ -99,14 +100,18 @@ class TLRUCache(_TimedCache[_KT, _VT]): def ttu(self) -> Callable[[_KT, _VT, float], float]: ... def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ... +class _Cached(Protocol[_R]): + __wrapped__: Callable[..., _R] + def __call__(self, *args: Any, **kwargs: Any) -> _R: ... + def cached( cache: MutableMapping[_KT, Any] | None, key: Callable[..., _KT] = ..., lock: AbstractContextManager[Any] | None = None, info: bool = False, -) -> IdentityFunction: ... +) -> Callable[[Callable[..., _R]], _Cached[_R]]: ... def cachedmethod( cache: Callable[[Any], MutableMapping[_KT, Any] | None], key: Callable[..., _KT] = ..., lock: Callable[[Any], AbstractContextManager[Any]] | None = None, -) -> IdentityFunction: ... +) -> Callable[[Callable[..., _R]], _Cached[_R]]: ... diff --git a/stubs/cachetools/cachetools/func.pyi b/stubs/cachetools/cachetools/func.pyi index 070383e58e27..3d51ba0251b3 100644 --- a/stubs/cachetools/cachetools/func.pyi +++ b/stubs/cachetools/cachetools/func.pyi @@ -1,19 +1,28 @@ -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, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False -) -> IdentityFunction: ... + maxsize: float | None = 128, *, typed: bool = False +) -> Callable[[Callable[..., _T]], _Cached[_T]]: ... +@overload +def rr_cache( + 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]]: ... From dac4b8be53a44ac54709519c1eef85ef194146f2 Mon Sep 17 00:00:00 2001 From: Mark Paronyan <markparonian@gmail.com> Date: Sat, 22 Mar 2025 10:36:01 +0000 Subject: [PATCH 3/9] tests: __wrapped__ attribute in cached functions --- tests/cachetools_wrapped_test.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/cachetools_wrapped_test.py diff --git a/tests/cachetools_wrapped_test.py b/tests/cachetools_wrapped_test.py new file mode 100644 index 000000000000..f3feb72ebc0a --- /dev/null +++ b/tests/cachetools_wrapped_test.py @@ -0,0 +1,11 @@ +from cachetools import cached, LRUCache +from typing import Any + +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) From 431141057f79d396650bc0c036847fb536b21d86 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Mar 2025 07:42:12 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks --- tests/cachetools_wrapped_test.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/cachetools_wrapped_test.py b/tests/cachetools_wrapped_test.py index f3feb72ebc0a..4f97376a8219 100644 --- a/tests/cachetools_wrapped_test.py +++ b/tests/cachetools_wrapped_test.py @@ -1,11 +1,14 @@ -from cachetools import cached, LRUCache 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) From 70a5914b11ff999fe7719fae0e074d36afc25f4e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 31 Mar 2025 17:40:08 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/cachetools/cachetools/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/cachetools/cachetools/__init__.pyi b/stubs/cachetools/cachetools/__init__.pyi index cc5b5c124f1b..1956d2ad29db 100644 --- a/stubs/cachetools/cachetools/__init__.pyi +++ b/stubs/cachetools/cachetools/__init__.pyi @@ -3,7 +3,7 @@ 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, ParamSpec +from typing_extensions import ParamSpec, deprecated __all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "MRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") __version__: str From 6a0f88c125d76ba37bb2ab58f12a78cd15fc8e40 Mon Sep 17 00:00:00 2001 From: Mark Paronyan <markparonian@gmail.com> Date: Mon, 31 Mar 2025 20:25:01 +0000 Subject: [PATCH 6/9] refactor: mv test to cachetools/@tests --- {tests => stubs/cachetools/@tests}/cachetools_wrapped_test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {tests => stubs/cachetools/@tests}/cachetools_wrapped_test.py (100%) diff --git a/tests/cachetools_wrapped_test.py b/stubs/cachetools/@tests/cachetools_wrapped_test.py similarity index 100% rename from tests/cachetools_wrapped_test.py rename to stubs/cachetools/@tests/cachetools_wrapped_test.py From 7a38deacb661dcb09d8ad990801bc84847494dcc Mon Sep 17 00:00:00 2001 From: Mark Paronyan <markparonian@gmail.com> Date: Mon, 31 Mar 2025 20:34:42 +0000 Subject: [PATCH 7/9] fix: use functools._Wrapped directly --- stubs/cachetools/cachetools/__init__.pyi | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/stubs/cachetools/cachetools/__init__.pyi b/stubs/cachetools/cachetools/__init__.pyi index 1d7eeefb6366..cc5b5c124f1b 100644 --- a/stubs/cachetools/cachetools/__init__.pyi +++ b/stubs/cachetools/cachetools/__init__.pyi @@ -1,8 +1,9 @@ from _typeshed import Unused from collections.abc import Callable, Iterator, MutableMapping, Sequence from contextlib import AbstractContextManager -from typing import Any, Protocol, TypeVar, overload -from typing_extensions import deprecated +from functools import _Wrapped +from typing import Any, TypeVar, overload +from typing_extensions import deprecated, ParamSpec __all__ = ("Cache", "FIFOCache", "LFUCache", "LRUCache", "MRUCache", "RRCache", "TLRUCache", "TTLCache", "cached", "cachedmethod") __version__: str @@ -11,6 +12,7 @@ _KT = TypeVar("_KT") _VT = TypeVar("_VT") _T = TypeVar("_T") _R = TypeVar("_R") +_P = ParamSpec("_P") class Cache(MutableMapping[_KT, _VT]): @overload @@ -100,18 +102,14 @@ class TLRUCache(_TimedCache[_KT, _VT]): def ttu(self) -> Callable[[_KT, _VT, float], float]: ... def expire(self, time: float | None = None) -> list[tuple[_KT, _VT]]: ... -class _Cached(Protocol[_R]): - __wrapped__: Callable[..., _R] - def __call__(self, *args: Any, **kwargs: Any) -> _R: ... - def cached( cache: MutableMapping[_KT, Any] | None, key: Callable[..., _KT] = ..., lock: AbstractContextManager[Any] | None = None, info: bool = False, -) -> Callable[[Callable[..., _R]], _Cached[_R]]: ... +) -> 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, -) -> Callable[[Callable[..., _R]], _Cached[_R]]: ... +) -> Callable[[Callable[_P, _R]], _Wrapped[_P, _R, _P, _R]]: ... From 017b4543c01db607318eadfe0811f102fdfcbb59 Mon Sep 17 00:00:00 2001 From: Mark Paronyan <markparonian@gmail.com> Date: Mon, 31 Mar 2025 20:37:10 +0000 Subject: [PATCH 8/9] fix(tests): mv cachetools test to test_cases --- .../cachetools/@tests/{ => test_cases}/cachetools_wrapped_test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename stubs/cachetools/@tests/{ => test_cases}/cachetools_wrapped_test.py (100%) diff --git a/stubs/cachetools/@tests/cachetools_wrapped_test.py b/stubs/cachetools/@tests/test_cases/cachetools_wrapped_test.py similarity index 100% rename from stubs/cachetools/@tests/cachetools_wrapped_test.py rename to stubs/cachetools/@tests/test_cases/cachetools_wrapped_test.py From f3393450504259118af7bd879a59a4128345343f Mon Sep 17 00:00:00 2001 From: Mark Paronyan <markparonian@gmail.com> Date: Mon, 31 Mar 2025 20:39:32 +0000 Subject: [PATCH 9/9] fix(tests): rename to check_cachetools_wrapped --- .../{cachetools_wrapped_test.py => check_cachetools_wrapped.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename stubs/cachetools/@tests/test_cases/{cachetools_wrapped_test.py => check_cachetools_wrapped.py} (100%) diff --git a/stubs/cachetools/@tests/test_cases/cachetools_wrapped_test.py b/stubs/cachetools/@tests/test_cases/check_cachetools_wrapped.py similarity index 100% rename from stubs/cachetools/@tests/test_cases/cachetools_wrapped_test.py rename to stubs/cachetools/@tests/test_cases/check_cachetools_wrapped.py