Skip to content

Commit 304167f

Browse files
committed
Fix type stubs for TTLCache and TLRUCache with default timer.
1 parent 505aece commit 304167f

3 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎src/cachetools/__init__.pyi‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ _T = TypeVar("_T")
3636
_KT = TypeVar("_KT")
3737
_VT = TypeVar("_VT")
3838
_TT = TypeVar("_TT", default=float)
39-
_KT2 = TypeVar("_KT2")
40-
_VT2 = TypeVar("_VT2")
4139

4240
class Cache(MutableMapping[_KT, _VT]):
4341
def __init__(
@@ -94,13 +92,13 @@ class _TimedCache(Cache[_KT, _VT], Generic[_KT, _VT, _TT]):
9492
def timer(self) -> _Timer[_TT]: ...
9593

9694
class TTLCache(_TimedCache[_KT, _VT, _TT]):
97-
@overload
95+
@overload # without timer, _TT takes its default
9896
def __init__(
99-
self: TTLCache[_KT2, _VT2, float],
97+
self,
10098
maxsize: float,
10199
ttl: float,
102100
*,
103-
getsizeof: Callable[[_VT2], float] | None = None,
101+
getsizeof: Callable[[_VT], float] | None = None,
104102
) -> None: ...
105103
@overload
106104
def __init__(
@@ -115,13 +113,13 @@ class TTLCache(_TimedCache[_KT, _VT, _TT]):
115113
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
116114

117115
class TLRUCache(_TimedCache[_KT, _VT, _TT]):
118-
@overload
116+
@overload # without timer, _TT takes its default
119117
def __init__(
120-
self: TLRUCache[_KT2, _VT2, float],
118+
self,
121119
maxsize: float,
122-
ttu: Callable[[_KT2, _VT2, float], float],
120+
ttu: Callable[[_KT, _VT, float], float],
123121
*,
124-
getsizeof: Callable[[_VT2], float] | None = None,
122+
getsizeof: Callable[[_VT], float] | None = None,
125123
) -> None: ...
126124
@overload
127125
def __init__(

‎tests/test_tlru.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,23 @@ def ttu(_k, _v, t):
400400
cache.timer.tick()
401401
cache.timer.tick() # past TTL
402402
self.assertNotIn(42, cache)
403+
404+
def test_tlru_default_timer_stubs(self):
405+
# type stubs must infer key and value types with the default timer
406+
def ttu(_key: str, value: int, now: float) -> float:
407+
return now + value
408+
409+
cache: TLRUCache[str, int] = TLRUCache(maxsize=2, ttu=ttu)
410+
411+
cache["a"] = 3600
412+
self.assertEqual(3600, cache["a"])
413+
self.assertEqual(1, len(cache))
414+
self.assertIn("a", cache)
415+
416+
sized: TLRUCache[str, str] = TLRUCache(
417+
maxsize=10, ttu=lambda _k, _v, now: now + 3600, getsizeof=len
418+
)
419+
420+
sized["abc"] = "abc"
421+
self.assertEqual("abc", sized["abc"])
422+
self.assertEqual(3, sized.currsize)

‎tests/test_ttl.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,18 @@ def test_ttl_clear(self):
272272
cache.timer.tick()
273273
cache.timer.tick() # past TTL
274274
self.assertNotIn(42, cache)
275+
276+
def test_ttl_default_timer_stubs(self):
277+
# type stubs must infer key and value types with the default timer
278+
cache: TTLCache[str, int] = TTLCache(maxsize=2, ttl=math.inf)
279+
280+
cache["a"] = 1
281+
self.assertEqual(1, cache["a"])
282+
self.assertEqual(1, len(cache))
283+
self.assertIn("a", cache)
284+
285+
sized: TTLCache[str, str] = TTLCache(maxsize=10, ttl=math.inf, getsizeof=len)
286+
287+
sized["abc"] = "abc"
288+
self.assertEqual("abc", sized["abc"])
289+
self.assertEqual(3, sized.currsize)

0 commit comments

Comments
 (0)