contextually-cached-property
A Python descriptor that caches property values per context variable, providing isolated caches for concurrent contexts like asyncio tasks.
Why?
Python's built-in functools.cached_property uses a single cache shared across all contexts. This can cause issues in concurrent applications where different tasks or threads need independent cached values.
contextually_cached_property solves this by using contextvars.ContextVar to maintain separate caches per context, while also using weak references to allow proper garbage collection of instances.
Installation
pip install contextually-cached-property
Requires Python 3.14+.
Usage
from contextually_cached_property import contextually_cached_property
class ExpensiveResource:
@contextually_cached_property
def connection(self) -> Connection:
return create_connection()
The cached value is computed once per context per instance. Different asyncio tasks will each get their own cached value:
import asyncio
class TaskLocalData:
@contextually_cached_property
def request_id(self) -> str:
return generate_unique_id()
data = TaskLocalData()
async def task_a():
print(data.request_id) # e.g., "abc123"
async def task_b():
print(data.request_id) # e.g., "xyz789" (different from task_a)
async def main():
await asyncio.gather(task_a(), task_b())
Invalidating the cache
Delete the attribute to clear the cached value for the current context:
del instance.cached_property_name
Development
This project uses Poetry for dependency management.
# Install dependencies
poetry install
# Run tests
pytest
# Run linting
ruff check
# Run type checking
mypy .
Releasing
This project uses git tags for versioning and automated publishing via Forgejo Actions.
Pre-release (TestPyPI)
Create a pre-release tag to publish to TestPyPI for testing:
git tag v1.0.0-rc1
git push origin v1.0.0-rc1
Production release (PyPI)
Create a release tag to publish to PyPI:
git tag v1.0.0
git push origin v1.0.0
The workflow runs tests, linting, and type checking before publishing.
License
See LICENSE for details.
Metadata
Release files for contextually-cached-property 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| contextually_cached_property-0.1.1.tar.gz | 14.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| contextually_cached_property-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.0 kB
Release files / contextually_cached_property-0.1.1.tar.gz
| Download URL | contextually_cached_property-0.1.1.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
514d70fcf34f0e948da3be6b9d0f5906e0ae4d210653040e6a581faf4d0151bd
|
|
BLAKE2b-256 checksum How to use checksums |
7b9fe2217ca18a141c9319c1181b2a1b1882651896f045296b0d58bcc34ab122
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.1 CPython/3.13.11 Linux/6.12.48+deb13-cloud-amd64
|
Release files / contextually_cached_property-0.1.1-py3-none-any.whl
| Download URL | contextually_cached_property-0.1.1-py3-none-any.whl |
|---|---|
| Size | 15.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c0a68f87f9441d16778e9249da53e2bdd90475dea448c22f9db2974b4501c719
|
|
BLAKE2b-256 checksum How to use checksums |
9301ef2508b83f16831aa9e0db6d3235d36c3d4d5253f40d7819df8bd51ecc54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.1 CPython/3.13.11 Linux/6.12.48+deb13-cloud-amd64
|