Skip to content

alluka.local#

Standard functions for using a local scope dependency injection client.

The "scope" will either be the current thread, an asynchronous runtime or an asynchronous event/task.

Note

This module's functionality will only work if initialize or scope_client has been called to set the DI client for the local scope.

initialise module-attribute #

initialise = initialize

Alias of initialize.

auto_inject #

auto_inject(callback)

Wrap a function to make calls to it always inject dependencies.

Examples:

@alluka.local.auto_inject
def callback(dep: Injected[Type]) -> None:
    ...

callback()  # The requested dependencies will be passed.

Parameters:

  • callback (Callable[_P, _T]) –

    The callback to wrap with DI.

Returns:

  • Callable

    The wrapped auto injecting callback.

auto_inject_async #

auto_inject_async(callback)

Wrap an async function to make calls to it always inject dependencies.

Examples:

@alluka.local.auto_inject_async
async def callback(dep: Injected[Type]) -> None:
    ...

await callback()  # The requested dependencies will be passed.

Parameters:

  • callback (Callable[_P, _CoroT[_T]]) –

    The callback to wrap with DI.

Returns:

  • Callable

    The wrapped auto injecting callback.

call_with_async_di async #

call_with_async_di(callback, *args, **kwargs)

Use the local client to call a callback with async DI.

Parameters:

  • callback (CallbackSig[_T]) –

    The callback to call.

  • *args (Any, default: () ) –

    Positional arguments to passthrough to the callback.

  • **kwargs (Any, default: {} ) –

    Keyword arguments to passthrough to the callback.

Returns:

  • _T

    The result of the call.

call_with_di #

call_with_di(callback, *args, **kwargs)

Use the local client to call a callback with DI.

Parameters:

  • callback (Callable[..., _T]) –

    The callback to call.

  • *args (Any, default: () ) –

    Positional arguments to passthrough to the callback.

  • **kwargs (Any, default: {} ) –

    Keyword arguments to passthrough to the callback.

Returns:

  • _T

    The result of the call.

get #

get(*, default=...)

Get the local client for the current scope.

Parameters:

  • default (_DefaultT, default: ... ) –

    The value to return if the client is not initialised.

    If not provided, a RuntimeError will be raised instead.

Returns:

  • Client | _DefaultT

    The client for the local scope, or the default value if the client is not initialised.

Raises:

  • RuntimeError

    If the client is not initialised and no default value was provided.

initialize #

initialize(client=None)

Link or initialise an injection client for the current scope.

This uses contextvars to store the client and therefore will not be inherited by child threads.

scope_client is recommended over this.

Parameters:

  • client (Optional[Client], default: None ) –

    If provided, this will be set as the client for the current scope. If not provided, a new client will be created.

Returns:

  • Client

    The created alluka client.

Raises:

  • RuntimeError

    If the local client is already initialised.

scope_client #

scope_client(client=None)

Declare a client for the scope within a context manager.

This uses contextvars to store the client and therefore will not be inherited by child threads.

Examples:

def uses_di() -> None:
    alluka.local.call_with_di(other_callback)

with alluka.local.scope_client() as client:
    client.set_type_dependency(Type, value)
    uses_di()

client = alluka.Client()
client.set_type_dependency(Type, value)

with alluka.local.scope_client(client):
    uses_di()

Parameters:

  • client (Optional[Client], default: None ) –

    The client to set for the context manager's scope.

    If not provided then a new client will be created.

Returns: