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.
Projecttkem/cachetools
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
| 41 | 41 | else: | |
| 42 | 42 | return cache_getitem(self, key) | |
| 43 | 43 | ||
| 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 | + | ): | |
| 45 | 51 | with self.timer as time: | |
| 46 | 52 | expires = self.__ttu(key, value, time) | |
| 47 | 53 | 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) | |
| 48 | 64 | return # skip expired items | |
| 49 | 65 | self.expire(time) | |
| 50 | 66 | cache_setitem(self, key, value) |
The check that tells the two apart
fail→pass·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
- 2026-03-08Fix #218: Fix and properly document @cachedmethod.cache_key handling.
- 2026-03-05_DescriptorBase
- 2024-08-18Fix #292, fix #205, fix #103: TTLCache.expire() returns iterable of expired (key, value) pairs.
- 2024-07-15mru_cache
- 2021-12-21Fix #159: Pass self to @cachedmethod key function.
- 2021-12-18TTLCache