Uncalled-for

0.2.0 · active · verified Wed Apr 01

Async dependency injection for Python functions. It allows declaring function dependencies as parameter defaults, which are then resolved when the function runs, offering a zero-ceremony, container-less, and configuration-free approach. The library is async-native and built on standard library features like `AsyncExitStack` and `ContextVar`. It supports context manager lifecycle for generators and nested dependencies, with caching to ensure each dependency resolves once per call. Current version is 0.2.0, with an active release cadence as seen from recent GitHub releases.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define an asynchronous dependency using an async generator, and then use `uncalled_for.call` to automatically resolve and inject this dependency into another asynchronous function. The `get_db_connection` acts as a context manager, ensuring resource cleanup.

import asyncio
from uncalled_for import call

# Define an async generator for a mock database connection
async def get_db_connection():
    print("Connecting to DB...")
    try:
        # Simulate a connection object
        yield "mock_db_connection_obj"
    finally:
        print("Closing DB connection...")

# Define a function that depends on the database connection
async def get_user_data(db_connection: str):
    print(f"Fetching user data using: {db_connection}")
    await asyncio.sleep(0.1) # Simulate async I/O
    return f"User data fetched with {db_connection}"

# Define the main application function
async def main_app_logic():
    # 'call' resolves get_db_connection and injects its yielded value into get_user_data
    data = await call(get_user_data, db_connection=get_db_connection)
    print(f"Received: {data}")

if __name__ == "__main__":
    asyncio.run(main_app_logic())

view raw JSON →