propcache

Fast, C-accelerated property caching for Python, providing `cached_property` and `under_cached_property` decorators

Library
PyPI
v0.5.2
38stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
65/100Good
Development Activity88
Maintenance80
Community28
Maturity44
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
68/100Good
Architecture65
Code Quality72
Innovation55
Learning Curve80

propcache provides a cached_property-style decorator that memoizes an expensive computed attribute on first access and reuses the result on subsequent lookups, with an optional Cython-compiled fast path for CPython and a pure-Python fallback for other interpreters like PyPy. It also offers under_cached_property, a variant that stores its cache under a name-mangled attribute to avoid clashing with __slots__ or public attribute names.

Extracted from the aio-libs ecosystem (the organization behind aiohttp, yarl, and multidict), propcache was split out as its own dependency so that performance-sensitive caching logic could be shared and independently versioned across those libraries and any other project needing the same pattern.

What You Get

  • cached_property decorator that memoizes a method’s result as an instance attribute on first access
  • under_cached_property variant storing the cache under a mangled name to coexist with __slots__
  • A Cython-compiled _helpers_c fast path used automatically on supported CPython builds
  • A pure-Python _helpers_py fallback so the library works unmodified on PyPy and other interpreters
  • Full type-hint support via a bundled py.typed marker for static type checkers

Common Use Cases

  • Caching expensive derived attributes (e.g. parsed URL components, computed hashes) on frequently instantiated objects
  • Reducing per-request overhead in high-throughput async libraries like aiohttp and yarl that call the same computed property repeatedly
  • Any Python class needing memoized properties without introducing a heavier caching framework as a dependency
  • Libraries needing memoization compatible with __slots__-based classes via under_cached_property

Under The Hood

Architecture The library exposes a stable public surface (api.py, __init__.py) that re-exports whichever implementation is available: a Cython-compiled extension built from _helpers_c.pyx when present, or the pure-Python _helpers_py.py implementation as an automatic fallback, with _helpers.py acting as the selection/compatibility shim between the two.

Tech Stack A hybrid Python/Cython project using a custom in-tree PEP 517 build backend (packaging/pep517_backend) to conditionally cythonize and compile the C extension at build time; ships prebuilt wheels for common platforms so most users never need a C compiler, and is packaged with a src/ layout.

Code Quality Despite its small size (roughly 250 lines across implementation files), the project has a proportionally thorough test suite (test_cached_property.py, test_under_cached_property.py, test_api.py, plus dedicated benchmark tests) and enforces mypy static typing via .mypy.ini, along with pre-commit hooks and towncrier-managed changelogs.

API Design The API is minimal by design — import cached_property and apply it exactly like Python’s standard-library functools.cached_property, making adoption immediate for anyone already familiar with that pattern, with under_cached_property as the only additional concept needed for __slots__ compatibility.

Used by 5 apps in this directory

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search