One function

TLRUCache in tkem/cachetools

The author described this change as Fix TLRUCache silently keeping stale value on expired overwrite. It counts as a record because the check below fails on the code as it stood at 978d34d40 and passes on c0fdf6aba, with nothing else changed between the two runs.

Fix saved2026-07-22
Sharing licenceMIT · LICENSE
Change size+17 1

What the code was meant to do, written into the code itself as a docstring

Time aware Least Recently Used (TLRU) cache implementation.

The change

4141 else:
4242 return cache_getitem(self, key)
4343
44- def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
44+ def __setitem__(
45+ self,
46+ key,
47+ value,
48+ cache_setitem=Cache.__setitem__,
49+ cache_delitem=Cache.__delitem__,
50+ ):
4551 with self.timer as time:
4652 expires = self.__ttu(key, value, time)
4753 if not (time < expires):
54+ # The new value is already expired, so it is not stored.
55+ # If the key still holds a previous (possibly still valid)
56+ # value, drop it as well -- otherwise the assignment would
57+ # be silently ignored and the stale value would persist.
58+ try:
59+ self.__items.pop(key).removed = True
60+ except KeyError:
61+ pass
62+ else:
63+ cache_delitem(self, key)
4864 return # skip expired items
4965 self.expire(time)
5066 cache_setitem(self, key, value)

The check that tells the two apart

failpass·tests/test_tlru.py::TLRUCacheTest::test_overwrite_existing_with_expired

Check file tests/test_tlru.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it978d34d40cdbf3b4cbfce104f3166032cc4ea028
Broken version dated2026-07-07
Modulecachetools.__init__
Units changedTLRUCache
Fingerprintab1abdd1e80dd1d5
Checked2026-08-17 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in tkem/cachetools