Registry / serialization / async-property

async-property

JSON →
library0.2.2pypypi✓ verified 24d ago

The `async-property` library provides Python decorators (`@async_property`, `@async_cached_property`) that enable defining asynchronous properties in classes. It allows getter methods to be coroutines, which can then be awaited when accessed. It also includes `AwaitLoader` for concurrently loading multiple cached async properties. The current version is 0.2.2, with a release cadence that appears maintenance-driven, having had releases in 2019 and 2023.

pip install async-property
INSTALL
IMPORT
SIG · ASYNC-PROPERTY
A
async-property
serializationpythonv0.2.2
Install
1.6s avg
Import
207ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.222s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.192s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

async_property
from async_property import async_property
async_cached_property
from async_property import async_cached_property
AwaitLoader
from async_property import AwaitLoader
from async_property.await_loader import AwaitLoader
AwaitLoader is directly exposed from the top-level package for convenience in recent versions.

This quickstart demonstrates the basic usage of `@async_property` for a standard awaitable property, `@async_cached_property` for a property that computes its value only once, and `AwaitLoader` for classes with multiple concurrently loaded cached properties. Remember to `await` the property access.

import asyncio from async_property import async_property, async_cached_property, AwaitLoader async def fetch_data(delay: float, value: str): await asyncio.sleep(delay) return value class MyClass: def __init__(self, initial_value: str): self._initial_value = initial_value @async_property async def regular_async_prop(self) -> str: print("Fetching regular_async_prop...") return await fetch_data(0.1, f"Regular: {self._initial_value}") @async_cached_property async def cached_async_prop(self) -> str: print("Fetching cached_async_prop (only once)...") return await fetch_data(0.2, f"Cached: {self._initial_value}") class MyAwaitableClass(AwaitLoader): def __init__(self, initial_value: str): self._initial_value = initial_value async def load(self): print("AwaitLoader load method called.") @async_cached_property async def await_loader_prop1(self) -> str: print("Fetching await_loader_prop1...") return await fetch_data(0.05, f"AwaitLoader Prop1: {self._initial_value}") @async_cached_property async def await_loader_prop2(self) -> str: print("Fetching await_loader_prop2...") return await fetch_data(0.03, f"AwaitLoader Prop2: {self._initial_value}") async def main(): instance = MyClass("test") # Accessing regular async property print(f"-> {await instance.regular_async_prop}") print(f"-> {await instance.regular_async_prop}") # Fetches again # Accessing cached async property print(f"-> {await instance.cached_async_prop}") print(f"-> {await instance.cached_async_prop}") # Returns cached value # Using AwaitLoader awaitable_instance = await MyAwaitableClass("loader_data") print(f"-> {awaitable_instance.await_loader_prop1}") print(f"-> {awaitable_instance.await_loader_prop2}") if __name__ == "__main__": asyncio.run(main())
Debug
Known issues
gotchaAsync properties do not support asynchronous setters. Python's `property` mechanism does not natively support async setters, and `async-property` only addresses async getters.
fix
Use a regular async method for setting values that require asynchronous operations (e.g., `await instance.set_remote_value(new_value)`).
affects: All versions
gotchaForgetting to `await` the async property will result in getting a coroutine object instead of the property's computed value. This is a common pitfall in `asyncio` applications.
fix
Always use `await` when accessing an `async_property` or `async_cached_property` (e.g., `await instance.my_async_property`).
affects: All versions
gotchaWhile `async-property` enables awaitable properties, the general Python community often advises against computationally expensive or I/O-bound operations in properties, as property access is semantically expected to be cheap and fast. Overusing async properties for heavy tasks might lead to less readable code or mask performance bottlenecks if not carefully designed.
fix
Evaluate whether an explicit `get_value()` async method might be more appropriate for highly expensive or complex I/O operations, reserving async properties for values that feel more like attributes.
affects: All versions
breakingPython 3.14 includes significant changes to `asyncio`'s internal implementation, particularly affecting nested event loops and workarounds like `nest_asyncio`. While `async-property` itself doesn't use `nest_asyncio`, issues related to `asyncio.run()` in environments like Jupyter notebooks on Python 3.14+ can indirectly affect how async properties are tested or used in such contexts.
fix
Ensure your `asyncio` environment is correctly set up for your Python version. In Jupyter/IPython with Python 3.14+, use `await` directly in cells instead of `asyncio.run()` for top-level execution. For `async-property`, ensure you always await its access.
affects: Python 3.14+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'async_property'
The 'async_property' module is not installed in the Python environment.
fix
Install the module using pip: 'pip install async-property'.
AttributeError: module 'async_property' has no attribute 'async_property'
Incorrect import statement; the correct attribute is 'async_property' from the 'async_property' module.
fix
Use the correct import: 'from async_property import async_property'.
TypeError: 'async_property' object is not callable
Attempting to call an 'async_property' decorated method directly instead of awaiting it.
fix
Access the property with 'await' syntax: 'await instance.property_name'.
SyntaxError: can't assign to await expression
Attempting to assign a value to an 'async_property' decorated property, which does not support assignment.
fix
Use a regular async method with explicit getter and setter methods instead of 'async_property'.
TypeError: object 'coroutine'/'_CachedAsyncProperty'/'_AsyncProperty' is not awaitable
You are trying to use the result of an `@async_property` or `@async_cached_property` directly without awaiting it, or you have applied the decorator to a non-async function.
fix
Ensure the decorated method is `async def` and always `await` the property when accessing its value. Example: `await instance.my_async_prop`.
Upgrade
Version history
0.2.2latest on PyPI · released Jul 3, 2023
Audit
Dependencies
wraptrequiredUsed for ObjectProxy functionality, which is part of the decorator's implementation.
Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources