Skip to content

Commit 293b140

Browse files
committed
Update func.pyi with precise cache decorator type hints
1 parent 67be89e commit 293b140

1 file changed

Lines changed: 45 additions & 10 deletions

File tree

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
1-
from _typeshed import IdentityFunction
21
from collections.abc import Callable, Sequence
3-
from typing import TypeVar
2+
from typing import Any, Final, Generic, NamedTuple, TypeVar, overload
3+
4+
__all__: Final = ("fifo_cache", "lfu_cache", "lru_cache", "rr_cache", "ttl_cache")
45

5-
__all__ = ("fifo_cache", "lfu_cache", "lru_cache", "rr_cache", "ttl_cache")
66
_T = TypeVar("_T")
7+
_R = TypeVar("_R")
8+
9+
class _CacheInfo(NamedTuple):
10+
hits: int
11+
misses: int
12+
maxsize: int | None
13+
currsize: int
14+
15+
class _cachetools_cache_wrapper(Generic[_R]):
16+
__wrapped__: Callable[..., _R]
17+
def __call__(self, /, *args: Any, **kwargs: Any) -> _R: ...
18+
def cache_info(self) -> _CacheInfo: ...
19+
def cache_clear(self) -> None: ...
20+
def cache_parameters(self) -> dict[str, Any]: ...
721

8-
def fifo_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ...
9-
def lfu_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ...
10-
def lru_cache(maxsize: float | None = 128, typed: bool = False) -> IdentityFunction: ...
22+
@overload
23+
def fifo_cache(
24+
maxsize: int | None = 128, typed: bool = False
25+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
26+
@overload
27+
def fifo_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
28+
@overload
29+
def lfu_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
30+
@overload
31+
def lfu_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
32+
@overload
33+
def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
34+
@overload
35+
def lru_cache(maxsize: Callable[..., _R], typed: bool = False) -> _cachetools_cache_wrapper[_R]: ...
36+
@overload
1137
def rr_cache(
12-
maxsize: float | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
13-
) -> IdentityFunction: ...
38+
maxsize: int | None = 128, choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
39+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
40+
@overload
41+
def rr_cache(
42+
maxsize: Callable[..., _R], choice: Callable[[Sequence[_T]], _T] | None = ..., typed: bool = False
43+
) -> _cachetools_cache_wrapper[_R]: ...
44+
@overload
45+
def ttl_cache(
46+
maxsize: int | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
47+
) -> Callable[[Callable[..., _R]], _cachetools_cache_wrapper[_R]]: ...
48+
@overload
1449
def ttl_cache(
15-
maxsize: float | None = 128, ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
16-
) -> IdentityFunction: ...
50+
maxsize: Callable[..., _R], ttl: float = 600, timer: Callable[[], float] = ..., typed: bool = False
51+
) -> _cachetools_cache_wrapper[_R]: ...

0 commit comments

Comments
 (0)