Skip to main content

No project description provided

Project description

fluent-async

Overview

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_style

Becomes:

async def example():
    await StyleBuilder().gray().bold().underline().as_posix_style

Features

  • Fluent API for Async Methods: Chain async methods seamlessly without manually awaiting each step.
  • Automatic Handling of Async Properties: Access @async_property attributes without explicit await.
  • Enhanced Debugging: Provides clear execution traces in __repr__ for better debugging.

Installation

pip install fluent-async
poetry add fluent-async

Usage

Basic Example

import 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())

How It Works

@fluent Decorator

  • Wraps an async method to allow fluent chaining.
  • Ensures returned values are wrapped in a Fluent instance.

Fluent Class

  • Implements __getattr__ to support accessing async properties.
  • Implements __call__ to handle method calls.
  • Overrides __await__ to return awaited values correctly.

License

MIT License -- LICENSE

Contributing

Contributions are welcome! Please submit issues or pull requests to improve the project.

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

fluent_async-0.1.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

fluent_async-0.1.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file fluent_async-0.1.2.tar.gz.

File metadata

  • Download URL: fluent_async-0.1.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.1 Linux/6.8.0-1020-azure

File hashes

Hashes for fluent_async-0.1.2.tar.gz
Algorithm Hash digest
SHA256 efb3b85a79342152de739ced3676253a8cc64df24a7c17ad8654c9ff73756cbb
MD5 35bc08ad3e687458947e3b97d3ea9fa7
BLAKE2b-256 30cfe1019069d2f2b03fcea5d70d0366963944a5aa80b01a9d0062493a0da41a

See more details on using hashes here.

File details

Details for the file fluent_async-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fluent_async-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.1 Linux/6.8.0-1020-azure

File hashes

Hashes for fluent_async-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dea22a1d5fe7814d349105339e9673548b54c31cf9b4907d8d6e5b9559d7d607
MD5 3f43e2cccb7ad81e7453189cd56d4f60
BLAKE2b-256 49568539e8baa081c13f5e60a4820bb330f3e631952e2594d0bebcd003a8ec62

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