cachetools 7.3.1__tar.gz → 7.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {cachetools-7.3.1 → cachetools-7.3.2}/CHANGELOG.rst +7 -0
  2. {cachetools-7.3.1 → cachetools-7.3.2}/MANIFEST.in +1 -0
  3. {cachetools-7.3.1/src/cachetools.egg-info → cachetools-7.3.2}/PKG-INFO +1 -1
  4. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/__init__.py +84 -22
  5. {cachetools-7.3.1 → cachetools-7.3.2/src/cachetools.egg-info}/PKG-INFO +1 -1
  6. {cachetools-7.3.1 → cachetools-7.3.2}/tests/__init__.py +55 -0
  7. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_fifo.py +4 -0
  8. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_lru.py +4 -0
  9. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_rr.py +15 -0
  10. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_tlru.py +27 -0
  11. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_ttl.py +30 -0
  12. {cachetools-7.3.1 → cachetools-7.3.2}/LICENSE +0 -0
  13. {cachetools-7.3.1 → cachetools-7.3.2}/README.rst +0 -0
  14. {cachetools-7.3.1 → cachetools-7.3.2}/docs/conf.py +0 -0
  15. {cachetools-7.3.1 → cachetools-7.3.2}/docs/index.rst +0 -0
  16. {cachetools-7.3.1 → cachetools-7.3.2}/pyproject.toml +0 -0
  17. {cachetools-7.3.1 → cachetools-7.3.2}/setup.cfg +0 -0
  18. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/__init__.pyi +0 -0
  19. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/_cached.py +0 -0
  20. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/_cachedmethod.py +0 -0
  21. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/_descriptor.py +0 -0
  22. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/func.py +0 -0
  23. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/func.pyi +0 -0
  24. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/keys.py +0 -0
  25. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/keys.pyi +0 -0
  26. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools/py.typed +0 -0
  27. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools.egg-info/SOURCES.txt +0 -0
  28. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools.egg-info/dependency_links.txt +0 -0
  29. {cachetools-7.3.1 → cachetools-7.3.2}/src/cachetools.egg-info/top_level.txt +0 -0
  30. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_cache.py +0 -0
  31. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_cached.py +0 -0
  32. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_cachedmethod.py +0 -0
  33. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_classmethod.py +0 -0
  34. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_func.py +0 -0
  35. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_keys.py +0 -0
  36. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_lfu.py +0 -0
  37. {cachetools-7.3.1 → cachetools-7.3.2}/tests/test_threading.py +0 -0
  38. {cachetools-7.3.1 → cachetools-7.3.2}/tox.ini +0 -0
@@ -1,3 +1,10 @@
1
+ v7.3.2 (2026-11-14)
2
+ ===================
3
+
4
+ - Properly support shallow copies of caches, including subclasses
5
+ with additional attributes (broken since v7.3.1).
6
+
7
+
1
8
  v7.3.1 (2026-11-12)
2
9
  ===================
3
10
 
@@ -1,3 +1,4 @@
1
1
  prune .github/
2
2
  exclude .gitignore
3
3
  exclude .readthedocs.yaml
4
+ exclude AGENTS.md
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.1
3
+ Version: 7.3.2
4
4
  Summary: Extensible memoizing collections and decorators
5
5
  Author-email: Thomas Kemmer <tkemmer@computer.org>
6
6
  Maintainer-email: Thomas Kemmer <tkemmer@computer.org>
@@ -12,7 +12,7 @@ __all__ = (
12
12
  "cachedmethod",
13
13
  )
14
14
 
15
- __version__ = "7.3.1"
15
+ __version__ = "7.3.2"
16
16
 
17
17
  import collections
18
18
  import collections.abc
@@ -112,11 +112,14 @@ class Cache(collections.abc.MutableMapping):
112
112
  def __len__(self):
113
113
  return len(self.__data)
114
114
 
115
- def __copy__(self, *shared):
116
- import copy
117
-
118
- shared = (self.getsizeof, *shared, *self.__data, *self.__data.values())
119
- return copy.deepcopy(self, {id(obj): obj for obj in shared})
115
+ def __copy__(self):
116
+ cls = type(self)
117
+ copy = cls.__new__(cls)
118
+ copy.__dict__.update(self.__dict__)
119
+ copy.__data = self.__data.copy()
120
+ if self.__size is not None:
121
+ copy.__size = self.__size.copy()
122
+ return copy
120
123
 
121
124
  # Note that we cannot simply inherit get(), pop() and setdefault()
122
125
  # from MutableMapping, since these rely on __getitem__ throwing a
@@ -194,6 +197,11 @@ class FIFOCache(Cache):
194
197
  cache_delitem(self, key)
195
198
  del self.__order[key]
196
199
 
200
+ def __copy__(self):
201
+ copy = super().__copy__()
202
+ copy.__order = self.__order.copy()
203
+ return copy
204
+
197
205
  def popitem(self):
198
206
  """Remove and return the `(key, value)` pair first inserted."""
199
207
  try:
@@ -223,6 +231,11 @@ class LFUCache(Cache):
223
231
  def __reduce__(self):
224
232
  return LFUCache._Link, (self.count,), (None, {"keys": self.keys})
225
233
 
234
+ def insert(self, next):
235
+ self.next = next
236
+ self.prev = prev = next.prev
237
+ prev.next = next.prev = self
238
+
226
239
  def unlink(self):
227
240
  next = self.next
228
241
  prev = self.prev
@@ -263,15 +276,27 @@ class LFUCache(Cache):
263
276
  if not link.keys:
264
277
  link.unlink()
265
278
 
279
+ def __copy__(self):
280
+ copy = super().__copy__()
281
+ copy.__root = root = LFUCache._Link(0)
282
+ root.prev = root.next = root
283
+ copy.__links = links = {}
284
+ curr = self.__root.next
285
+ while curr is not self.__root:
286
+ link = LFUCache._Link(curr.count)
287
+ link.keys = curr.keys.copy()
288
+ link.insert(root)
289
+ links.update(dict.fromkeys(link.keys, link))
290
+ curr = curr.next
291
+ return copy
292
+
266
293
  def __setstate__(self, state):
267
294
  self.__dict__.update(state)
268
295
  root = self.__root
269
296
  root.prev = root.next = root
270
297
  links = set(self.__links.values())
271
298
  for link in sorted(links, key=lambda obj: obj.count):
272
- link.next = root
273
- link.prev = prev = root.prev
274
- prev.next = root.prev = link
299
+ link.insert(root)
275
300
 
276
301
  def popitem(self):
277
302
  """Remove and return the `(key, value)` pair least frequently used."""
@@ -332,6 +357,11 @@ class LRUCache(Cache):
332
357
  cache_delitem(self, key)
333
358
  del self.__order[key]
334
359
 
360
+ def __copy__(self):
361
+ copy = super().__copy__()
362
+ copy.__order = self.__order.copy()
363
+ return copy
364
+
335
365
  def popitem(self):
336
366
  """Remove and return the `(key, value)` pair least recently used."""
337
367
  try:
@@ -364,8 +394,11 @@ class RRCache(Cache):
364
394
  self.__index = {}
365
395
  self.__keys = []
366
396
 
367
- def __copy__(self, *shared):
368
- return Cache.__copy__(self, self.__choice, *shared)
397
+ def __copy__(self):
398
+ copy = super().__copy__()
399
+ copy.__index = self.__index.copy()
400
+ copy.__keys = self.__keys.copy()
401
+ return copy
369
402
 
370
403
  @property
371
404
  def choice(self):
@@ -428,6 +461,9 @@ class _TimedCache(Cache):
428
461
  def __exit__(self, *exc):
429
462
  self.__nesting -= 1
430
463
 
464
+ def __copy__(self):
465
+ return _TimedCache._Timer(self.__timer)
466
+
431
467
  def __reduce__(self):
432
468
  return _TimedCache._Timer, (self.__timer,)
433
469
 
@@ -438,9 +474,10 @@ class _TimedCache(Cache):
438
474
  Cache.__init__(self, maxsize, getsizeof)
439
475
  self.__timer = _TimedCache._Timer(timer)
440
476
 
441
- def __copy__(self, *shared):
442
- # share the timer function, which is all a _Timer reduces to
443
- return Cache.__copy__(self, *self.__timer.__reduce__()[1], *shared)
477
+ def __copy__(self):
478
+ copy = super().__copy__()
479
+ copy.__timer = self.__timer.__copy__()
480
+ return copy
444
481
 
445
482
  def __repr__(self, cache_repr=Cache.__repr__):
446
483
  with self.__timer as time:
@@ -498,6 +535,11 @@ class TTLCache(_TimedCache):
498
535
  def __reduce__(self):
499
536
  return TTLCache._Link, (self.key, self.expires)
500
537
 
538
+ def insert(self, next):
539
+ self.next = next
540
+ self.prev = prev = next.prev
541
+ prev.next = next.prev = self
542
+
501
543
  def unlink(self):
502
544
  next = self.next
503
545
  prev = self.prev
@@ -544,9 +586,7 @@ class TTLCache(_TimedCache):
544
586
  links.move_to_end(key)
545
587
  link.unlink()
546
588
  link.expires = expires
547
- link.next = root = self.__root
548
- link.prev = prev = root.prev
549
- prev.next = root.prev = link
589
+ link.insert(self.__root)
550
590
 
551
591
  def __delitem__(self, key, cache_delitem=Cache.__delitem__):
552
592
  cache_delitem(self, key)
@@ -565,14 +605,27 @@ class TTLCache(_TimedCache):
565
605
  yield curr.key
566
606
  curr = curr.next
567
607
 
608
+ def __copy__(self):
609
+ copy = super().__copy__()
610
+ copy.__root = root = TTLCache._Link()
611
+ root.prev = root.next = root
612
+ links = {}
613
+ curr = self.__root.next
614
+ while curr is not self.__root:
615
+ links[curr.key] = link = TTLCache._Link(curr.key, curr.expires)
616
+ link.insert(root)
617
+ curr = curr.next
618
+ copy.__links = collections.OrderedDict(
619
+ (key, links[key]) for key in self.__links
620
+ )
621
+ return copy
622
+
568
623
  def __setstate__(self, state):
569
624
  self.__dict__.update(state)
570
625
  root = self.__root
571
626
  root.prev = root.next = root
572
627
  for link in sorted(self.__links.values(), key=lambda obj: obj.expires):
573
- link.next = root
574
- link.prev = prev = root.prev
575
- prev.next = root.prev = link
628
+ link.insert(root)
576
629
  self.expire(self.timer())
577
630
 
578
631
  @property
@@ -705,8 +758,17 @@ class TLRUCache(_TimedCache):
705
758
  if time < curr.expires and not curr.removed:
706
759
  yield curr.key
707
760
 
708
- def __copy__(self, *shared):
709
- return _TimedCache.__copy__(self, self.__ttu, *shared)
761
+ def __copy__(self):
762
+ copy = super().__copy__()
763
+ items = {}
764
+ for item in self.__order:
765
+ items[item] = new = TLRUCache._Item(item.key, item.expires)
766
+ new.removed = item.removed
767
+ copy.__order = [items[item] for item in self.__order]
768
+ copy.__items = collections.OrderedDict(
769
+ (key, items[item]) for key, item in self.__items.items()
770
+ )
771
+ return copy
710
772
 
711
773
  @property
712
774
  def ttu(self):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.1
3
+ Version: 7.3.2
4
4
  Summary: Extensible memoizing collections and decorators
5
5
  Author-email: Thomas Kemmer <tkemmer@computer.org>
6
6
  Maintainer-email: Thomas Kemmer <tkemmer@computer.org>
@@ -541,6 +541,61 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
541
541
  self.assertEqual(n, len(cache))
542
542
  self.assertEqual(source, cache)
543
543
 
544
+ def test_copy_attributes(self):
545
+ import copy
546
+ import threading
547
+
548
+ class Cache(self.Cache):
549
+ def __init__(self, maxsize, **kwargs):
550
+ super().__init__(maxsize, **kwargs)
551
+ self.lock = threading.Lock() # cannot be deep-copied
552
+ self.stats = {"hits": 0}
553
+
554
+ source = Cache(maxsize=2)
555
+ source.update({1: [1], 2: [2]})
556
+
557
+ cache = copy.copy(source)
558
+ self.assertIs(type(source), type(cache))
559
+ # a shallow copy shares attributes and values with its source
560
+ self.assertIs(source.lock, cache.lock)
561
+ self.assertIs(source.stats, cache.stats)
562
+ self.assertIs(source[1], cache[1])
563
+ self.assertIs(source[2], cache[2])
564
+
565
+ # but none of the cache's own state
566
+ cache[3] = [3]
567
+ del cache[next(iter(cache))]
568
+ self.assertEqual(1, len(cache))
569
+ self.assertEqual(2, len(source))
570
+ self.assertEqual(2, source.currsize)
571
+ self.assertIn(1, source)
572
+ self.assertIn(2, source)
573
+ self.assertNotIn(3, source)
574
+
575
+ def test_copy_getsizeof(self):
576
+ import copy
577
+
578
+ class GetSizeOf:
579
+ def __call__(self, value):
580
+ return value
581
+
582
+ source = self.Cache(maxsize=3, getsizeof=GetSizeOf())
583
+ source.update({1: 1, 2: 2})
584
+
585
+ cache = copy.copy(source)
586
+ self.assertIs(source.getsizeof, cache.getsizeof)
587
+ self.assertEqual(3, cache.currsize)
588
+
589
+ # item sizes are kept separately for source and copy
590
+ cache[2] = 1
591
+ self.assertEqual(2, cache.currsize)
592
+ self.assertEqual(3, source.currsize)
593
+ del source[2]
594
+ self.assertEqual(1, source.currsize)
595
+ self.assertEqual(2, cache.currsize)
596
+ del cache[2]
597
+ self.assertEqual(1, cache.currsize)
598
+
544
599
  def test_deepcopy(self):
545
600
  import copy
546
601
 
@@ -9,6 +9,8 @@ class FIFOCacheTest(unittest.TestCase, CacheTestMixin):
9
9
  Cache = FIFOCache
10
10
 
11
11
  def test_fifo(self):
12
+ import copy
13
+
12
14
  cache = FIFOCache[int, int](maxsize=2)
13
15
 
14
16
  cache[1] = 1
@@ -21,6 +23,8 @@ class FIFOCacheTest(unittest.TestCase, CacheTestMixin):
21
23
  self.assertNotIn(1, cache)
22
24
 
23
25
  cache[2]
26
+ # a shallow copy evicts in the same order, on its own
27
+ self.assertEqual((2, 2), copy.copy(cache).popitem())
24
28
  cache[4] = 4
25
29
  self.assertEqual(len(cache), 2)
26
30
  self.assertEqual(cache[3], 3)
@@ -9,6 +9,8 @@ class LRUCacheTest(unittest.TestCase, CacheTestMixin):
9
9
  Cache = LRUCache
10
10
 
11
11
  def test_lru(self):
12
+ import copy
13
+
12
14
  cache = LRUCache[int, int](maxsize=2)
13
15
 
14
16
  cache[1] = 1
@@ -21,6 +23,8 @@ class LRUCacheTest(unittest.TestCase, CacheTestMixin):
21
23
  self.assertNotIn(1, cache)
22
24
 
23
25
  cache[2]
26
+ # a shallow copy evicts in the same order, on its own
27
+ self.assertEqual((3, 3), copy.copy(cache).popitem())
24
28
  cache[4] = 4
25
29
  self.assertEqual(len(cache), 2)
26
30
  self.assertEqual(cache[2], 2)
@@ -101,3 +101,18 @@ class RRCacheTest(unittest.TestCase, CacheTestMixin):
101
101
 
102
102
  with self.assertRaises(KeyError):
103
103
  cache.popitem()
104
+
105
+ def test_rr_copy(self):
106
+ import copy
107
+
108
+ source = RRCache[int, int](maxsize=3, choice=min)
109
+ source.update({1: 1, 2: 2, 3: 3})
110
+
111
+ # the copy has its own keys to choose from
112
+ cache = copy.copy(source)
113
+ del cache[1]
114
+ self.assertEqual((2, 2), cache.popitem())
115
+ self.assertEqual((1, 1), source.popitem())
116
+ self.assertEqual((2, 2), source.popitem())
117
+ self.assertEqual((3, 3), source.popitem())
118
+ self.assertEqual((3, 3), cache.popitem())
@@ -420,3 +420,30 @@ class TLRUCacheTest(unittest.TestCase, CacheTestMixin):
420
420
  sized["abc"] = "abc"
421
421
  self.assertEqual("abc", sized["abc"])
422
422
  self.assertEqual(3, sized.currsize)
423
+
424
+ def test_tlru_copy(self):
425
+ import copy
426
+
427
+ source = TLRUCache[int, int, int](
428
+ maxsize=3, ttu=lambda _key, value, time: time + value, timer=Timer()
429
+ )
430
+ source[1] = 3
431
+ source[2] = 1
432
+ source[3] = 2
433
+ source[1] = 2 # leaves a removed item behind, least recently used is 2
434
+
435
+ cache = copy.copy(source)
436
+ self.assertEqual(list(source), list(cache))
437
+
438
+ # the copy evicts as its source would, on its own
439
+ cache[4] = 5
440
+ self.assertEqual([1, 3, 4], sorted(cache))
441
+ self.assertEqual([1, 2, 3], sorted(source))
442
+
443
+ # items expire in the copy as in its source
444
+ cache.timer.tick()
445
+ self.assertEqual([1, 3], sorted(source))
446
+ self.assertEqual([1, 3, 4], sorted(cache))
447
+ source.timer.tick()
448
+ self.assertEqual([], list(source))
449
+ self.assertEqual([4], list(cache))
@@ -287,3 +287,33 @@ class TTLCacheTest(unittest.TestCase, CacheTestMixin):
287
287
  sized["abc"] = "abc"
288
288
  self.assertEqual("abc", sized["abc"])
289
289
  self.assertEqual(3, sized.currsize)
290
+
291
+ def test_ttl_copy(self):
292
+ import copy
293
+
294
+ source = TTLCache[int, int, int](maxsize=3, ttl=2, timer=Timer())
295
+ source[1] = 1
296
+ source[2] = 2
297
+ source.timer.tick()
298
+ source[3] = 3
299
+ self.assertEqual(1, source[1]) # least recently used is now 2
300
+
301
+ cache = copy.copy(source)
302
+ self.assertEqual([1, 2, 3], list(cache))
303
+
304
+ # the copy evicts as its source would, on its own
305
+ cache[4] = 4
306
+ self.assertEqual([1, 3, 4], list(cache))
307
+ self.assertEqual([1, 2, 3], list(source))
308
+
309
+ # the timer is shared, but not the caches' use of it
310
+ self.assertIsNot(source.timer, cache.timer)
311
+ with source.timer:
312
+ cache.timer.tick()
313
+ self.assertEqual(1, source.timer())
314
+ self.assertEqual(2, cache.timer())
315
+ self.assertEqual(2, source.timer())
316
+
317
+ # items expire in the copy as in its source
318
+ self.assertEqual([3, 4], list(cache))
319
+ self.assertEqual([3], list(source))
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes