Skip to main content

A cached property decorator with context-aware isolation

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextually_cached_property-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contextually_cached_property-0.1.0-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file contextually_cached_property-0.1.0.tar.gz.

File metadata

  • Download URL: contextually_cached_property-0.1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.12.48+deb13-cloud-amd64

File hashes

Hashes for contextually_cached_property-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3f7947e6015055baa9b4b13a8d6669a7855548b1c58b1337509aca5e99c9109c
MD5 f60ecba61761acc7b7d49ce0bbbed45a
BLAKE2b-256 564c5e62da3a2e2b07573518ed59ed816c45feddb8d4c7754fa8bf6740c9a472

See more details on using hashes here.

File details

Details for the file contextually_cached_property-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for contextually_cached_property-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67fd19e84144ceb5e81af08b6b32923f0c3019b7d930ed3f074a781335d07c80
MD5 239542b3cd9d440fb5046aa367e5b764
BLAKE2b-256 cc02f5da9e3464d429bcfb1d876cc803b1834176f66572e61d9c25fb114a89d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page