Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions stubs/cachetools/@tests/test_cases/check_cachetools.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from __future__ import annotations

from collections.abc import Hashable
from typing import Any
from typing import Any, TypeVar
from typing_extensions import assert_type

from cachetools import LRUCache, cached, keys as cachekeys
from cachetools import LRUCache, TLRUCache, TTLCache, cached, keys as cachekeys
from cachetools.func import fifo_cache, lfu_cache, lru_cache, rr_cache, ttl_cache

_KT = TypeVar("_KT")
_VT = TypeVar("_VT")

# Tests for cachetools.cached

# Explicitly parameterize the cache to avoid Unknown types
Expand Down Expand Up @@ -102,3 +105,28 @@ def method(self, a: int) -> int:

k4 = cachekeys.typedmethodkey(inst, 2)
assert_type(k4, tuple[Hashable, ...])


# Tests for TTLCache / TLRUCache constructors
# See https://github.com/python/typeshed/issues/15798

ttl_inst: TTLCache[str, int] = TTLCache(maxsize=128, ttl=600)
assert_type(ttl_inst, TTLCache[str, int, float])
assert_type(TTLCache[str, int](maxsize=128, ttl=600), TTLCache[str, int, float])
ttl_sized: TTLCache[str, bytes] = TTLCache(maxsize=128, ttl=600, getsizeof=len)
ttl_timer: TTLCache[str, int, int] = TTLCache(maxsize=128, ttl=600, timer=lambda: 1)
assert_type(ttl_timer, TTLCache[str, int, int])

tlru_inst: TLRUCache[str, int] = TLRUCache(10, lambda k, v, now: now + 10)
assert_type(tlru_inst, TLRUCache[str, int, float])
assert_type(TLRUCache[str, int](10, lambda k, v, now: now + 10), TLRUCache[str, int, float])


class _TrackedTTLCache(TTLCache[_KT, _VT]):
def popitem(self) -> tuple[_KT, _VT]:
key, value = super().popitem()
return key, value


tracked: _TrackedTTLCache[str, int] = _TrackedTTLCache(maxsize=128, ttl=600)
assert_type(tracked, _TrackedTTLCache[str, int])
14 changes: 8 additions & 6 deletions stubs/cachetools/cachetools/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ _VT = TypeVar("_VT")
_TT = TypeVar("_TT", default=float)
_T = TypeVar("_T")
_R = TypeVar("_R")
_KT2 = TypeVar("_KT2")
_VT2 = TypeVar("_VT2")

class Cache(MutableMapping[_KT, _VT]):
def __init__(self, maxsize: float, getsizeof: Callable[[_VT], float] | None = None) -> None: ...
Expand Down Expand Up @@ -66,7 +64,11 @@ class _TimedCache(Cache[_KT, _VT], Generic[_KT, _VT, _TT]):
class TTLCache(_TimedCache[_KT, _VT, _TT]):
@overload
def __init__(
self: TTLCache[_KT2, _VT2, float], maxsize: float, ttl: float, *, getsizeof: Callable[[_VT2], float] | None = None
self: TTLCache[_KT, _VT, float], # pyright: ignore[reportInvalidTypeVarUse] #11780
maxsize: float,
ttl: float,
*,
getsizeof: Callable[[_VT], float] | None = None,
) -> None: ...
@overload
def __init__(
Expand All @@ -84,11 +86,11 @@ class TTLCache(_TimedCache[_KT, _VT, _TT]):
class TLRUCache(_TimedCache[_KT, _VT, _TT]):
@overload
def __init__(
self: TLRUCache[_KT2, _VT2, float],
self: TLRUCache[_KT, _VT, float], # pyright: ignore[reportInvalidTypeVarUse] #11780
maxsize: float,
ttu: Callable[[_KT2, _VT2, float], float],
ttu: Callable[[_KT, _VT, float], float],
*,
getsizeof: Callable[[_VT2], float] | None = None,
getsizeof: Callable[[_VT], float] | None = None,
) -> None: ...
@overload
def __init__(
Expand Down
Loading