Skip to main content

Typed cached property decorator with async support

Project description

cacheprop

PyPI

A typed cached property decorator with async support.

Why?

  • Makes caching of time or computation-expensive properties quick and easy.
  • Provides proper type checking via PEP 695 generic syntax (class cacheprop[T]).
  • Works with both sync and async methods — the return value is automatically awaited for async functions.
  • Preserves __doc__ and __annotations__ from the wrapped function.

Installation

pip install cacheprop

Requires Python 3.12+ (uses PEP 695 type parameter syntax).

Usage

Sync methods

from cacheprop import cacheprop


class MyClass:
    @cacheprop
    def expensive(self) -> str:
        return heavy_computation()


obj = MyClass()
print(obj.expensive)  # calls heavy_computation(), caches result
print(obj.expensive)  # returns cached value, no call

Async methods

class MyAsyncClass:
    @cacheprop
    async def fetch_data(self) -> dict:
        return await http_get("https://example.com/api")


async def main():
    obj = MyAsyncClass()
    data1 = await obj.fetch_data  # awaits and caches
    data2 = await obj.fetch_data  # returns cached value, no await needed

Reusing an existing function

def get_absolute_url(self):
    return f"/items/{self.id}"


class Item:
    url = cacheprop(get_absolute_url)

Type checking

The generic parameter T lets type checkers infer the return type:

@cacheprop
def size(self) -> int:
    return len(self.data)

reveal_type(obj.size)  # int (not object or Any)

License

0BSD

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

cacheprop-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

cacheprop-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cacheprop-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cacheprop-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2214601bce4eb882d35906d33ded19ac4ca951c63da91db9733dd93238655af9
MD5 aa921ab159f8cbcdee39523456a4df85
BLAKE2b-256 c05ff49c89eb9d312eb139e30520f0783c10c8658ce0da43ea3347cb7cf7156d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cacheprop-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cacheprop-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 771b0050ff8c1cd1720caa89318a60a6ef7f234cdb2276679e5c06e649146560
MD5 6b34ef01d2584650efbc00060701c6c9
BLAKE2b-256 1be7d01bd3fb069f8498db405b9f74fa42249347ca6b4fd7ceaf2a395c20f4c4

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