Registry / serialization / backports-functools-lru-cache

backports-functools-lru-cache

JSON →
library2.0.0pypypi✓ verified 24d ago

This library provides a backport of the `functools.lru_cache` decorator, originally introduced in Python 3.2, primarily for use in older Python environments (e.g., Python 2.7, 3.2-3.5). For Python versions 3.8 and newer, it acts as a no-op, internally importing the built-in `functools.lru_cache` for compatibility. The current version is 2.0.0, released in December 2023, with updates occurring on an as-needed basis rather than a fixed cadence.

pip install backports-functools-lru-cache
INSTALL
IMPORT
SIG · BACKPORTS-FUNCTOOL
B
backports-functools-lru-cache
serializationpythonv2.0.0
Install
1.9s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

lru_cache
try: from functools import lru_cache except ImportError: from backports.functools_lru_cache import lru_cache
from backports.functools_lru_cache import lru_cache
The recommended approach uses a try-except block to prefer the built-in `lru_cache` on Python 3.2+ (or Python 3.8+ for direct library compatibility) and fall back to the backport for older versions. Directly importing from the backport when the built-in is available is generally unnecessary.

Demonstrates caching an expensive function using the `@lru_cache` decorator. The second call to `expensive_computation(5)` will retrieve the result from the cache, resulting in a much faster execution time.

import time try: from functools import lru_cache except ImportError: from backports.functools_lru_cache import lru_cache @lru_cache(maxsize=128) def expensive_computation(n): """Simulates an expensive computation.""" time.sleep(0.1) # Simulate work return n * n print("First call:") start = time.perf_counter() result1 = expensive_computation(5) end = time.perf_counter() print(f"Result: {result1}, Time taken: {end - start:.4f}s") print("\nSecond call (should be cached):") start = time.perf_counter() result2 = expensive_computation(5) end = time.perf_counter() print(f"Result: {result2}, Time taken: {end - start:.4f}s") print(f"\nCache Info: {expensive_computation.cache_info()}")
Debug
Known issues
gotchaThis library is primarily intended for Python versions older than 3.8 (or more generally, older than 3.2, where `lru_cache` was introduced). On Python 3.8+ environments, installing and using this package is largely a no-op, as it will simply re-export the built-in `functools.lru_cache`.
fix
On Python 3.8+, you can usually import `lru_cache` directly from `functools`. If targeting multiple Python versions, use the `try-except` import pattern shown in the `imports` section.
affects: <2.0.0 on Python 3.8+, >=2.0.0 on Python 3.8+
gotchaThe `lru_cache` (both built-in and backported) stores references to the return values. If a mutable object (like a list or dictionary) is returned and subsequently modified, the cached value will also reflect these changes, potentially leading to incorrect behavior upon future cache hits.
fix
Ensure that cached functions return immutable objects (e.g., tuples, frozensets, copies of mutable objects) or design your application to account for mutable cached state. Alternatively, consider `functools.cached_property` for instance-specific caching on methods that return mutable types.
affects: All versions
gotchaWhen applying `@lru_cache` to methods of dataclasses (especially frozen ones), the cache key might be based on the instance's hash. If multiple instances hash to the same value (common with frozen dataclasses that equate/hash based on field values), the cache can become shared across instances, causing method calls on different objects to return the same cached result.
fix
For instance-specific caching on dataclass methods, `functools.cached_property` is often a more appropriate choice. If `lru_cache` is strictly required, ensure that dataclass instances have unique and stable hash values that correctly differentiate them for caching purposes.
affects: All versions
Upgrade
Version history
2.0.0latest on PyPI · released Dec 13, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources
backports-functools-lru-cache — pip install backports-functools-lru-cache · libregistry