fluent-async is a Python library that enables fluent-style chaining of asynchronous methods and supports @async_property and @async_cached_property without requiring explicit await statements at each step. It simplifies working with asynchronous classes and methods by eliminating deep nesting of await expressions.
In other words:
async def example():
await (await (await (await StyleBuilder().gray()).bold()).underline()).as_posix_styleBecomes:
async def example():
await StyleBuilder().gray().bold().underline().as_posix_style- Fluent API for Async Methods: Chain async methods seamlessly without manually awaiting each step.
- Automatic Handling of Async Properties: Access
@async_propertyattributes without explicitawait. - Enhanced Debugging: Provides clear execution traces in
__repr__for better debugging.
pip install fluent-asyncpoetry add fluent-asyncimport asyncio
from async_property import async_property
from fluent_async import fluent
from typing import cast, Callable, Self
class Arithmetic:
def __init__(self, value: int):
self.value = value
@fluent
async def increment(self) -> Self:
self.value += 1
return self
increment = cast(Callable[..., Self], increment)
@fluent
async def double(self) -> Self:
self.value *= 2
return self
double = cast(Callable[..., Self], double)
@async_property
async def async_value(self) -> int:
return self.value
async def main():
result = await Arithmetic(1).increment().double().async_value
print(result) # Expected output: (1+1) * 2 = 4
asyncio.run(main())- Wraps an async method to allow fluent chaining.
- Ensures returned values are wrapped in a
Fluentinstance.
- Implements
__getattr__to support accessing async properties. - Implements
__call__to handle method calls. - Overrides
__await__to return awaited values correctly.
MIT License -- LICENSE
Contributions are welcome! Please submit issues or pull requests to improve the project.