propcache

raw JSON →
0.4.1 verified Tue May 12 auth: no python install: verified quickstart: verified

propcache is a Python library that provides accelerated implementations of cached properties, enhancing performance for property caching in Python 3.9 and above. The current version is 0.4.1, with a release cadence that includes regular updates and bug fixes.

pip install propcache
error ModuleNotFoundError: No module named 'propcache'
cause The propcache library is not installed in the current Python environment.
fix
Run pip install propcache to install the library.
error AttributeError: cached property is read-only
cause You are attempting to directly assign a new value to a property decorated with `propcache.cached_property` or `propcache.under_cached_property`, which are designed to be computed once and then accessed, not directly modified after first access.
fix
If you need to re-evaluate the property, delete the cached attribute from the instance's dictionary (e.g., del self.__dict__['my_property']) or the dedicated cache (del self._cache['my_property'] for under_cached_property), and then access the property again. Do not attempt to assign to it directly.
error error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
cause propcache uses Cython for accelerated implementations, and if pre-built binary wheels are not available for your specific system/Python version, it attempts to compile from source, requiring a C compiler. This specific error occurs on Windows when the necessary build tools are missing.
fix
Install the required C/C++ build tools. On Windows, download and install 'Microsoft C++ Build Tools' from the provided link. On Linux, ensure gcc and Python development headers (e.g., python3-dev or python-devel) are installed. Alternatively, you can force a slower pure-Python installation by running pip install propcache --config-settings=pure-python=true or setting the environment variable PROPCACHE_NO_EXTENSIONS=1 before installation.
breaking Version 0.4.0 was yanked due to a reference leak caused by improper reference counting in Cython.
fix Upgrade to version 0.4.1 or later, where the issue has been resolved.
gotcha propcache's under_cached_property uses self._cache instead of self.__dict__ to store cached values, which may lead to unexpected behavior if self._cache is not properly managed.
fix Ensure that self._cache is correctly initialized and managed within your class when using under_cached_property.
gotcha The provided test output contains warnings from pip itself regarding running as root and available updates, which are environmental and do not indicate a failure of the library under test. No library-specific failure is described.
fix Address pip warnings if necessary, but this is not a library-specific failure.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.01s 18.0M
3.10 slim (glibc) - - 0.01s 18M
3.11 alpine (musl) - - 0.02s 19.8M
3.11 slim (glibc) - - 0.01s 20M
3.12 alpine (musl) - - 0.01s 11.7M
3.12 slim (glibc) - - 0.01s 12M
3.13 alpine (musl) - - 0.01s 11.4M
3.13 slim (glibc) - - 0.01s 12M
3.9 alpine (musl) - - 0.01s 17.8M
3.9 slim (glibc) - - 0.01s 18M

This example demonstrates how to use propcache's cached_property to cache the result of an expensive computation in a class property.

from propcache import cached_property

class MyClass:
    def __init__(self, value):
        self._value = value

    @cached_property
    def computed_property(self):
        # Expensive computation
        return self._value * 2

obj = MyClass(10)
print(obj.computed_property)  # Outputs: 20