lightbulb.di.solver

class AutoInjecting(func: Callable[..., Awaitable[t.Any]], self_: t.Any = None, _cached_pos_or_kw_params: list[tuple[str, t.Any]] | None = None, _cached_kw_only_params: dict[str, t.Any] | None = None)[source]

Wrapper for a callable that implements dependency injection. When called, resolves the required dependencies and calls the original callable. Supports both synchronous and asynchronous functions, however this cannot be called synchronously - synchronous functions will need to be awaited.

You should generally never have to instantiate this yourself - you should instead use one of the decorators that applies this to the target automatically.

class AutocompleteContainer

Injectable type representing the dependency container for the autocomplete context.

alias of Container

class CommandContainer

Injectable type representing the dependency container for the command context.

alias of Container

class Context

Type representing a Lightbulb dependency injection context. Used for labelling containers so that they can be accessed using method injection if required. You can create and use your own contexts by creating an instance of this type.

alias of str

final class Contexts[source]

Collection of the dependency injection context values Lightbulb uses.

AUTOCOMPLETE = 'lightbulb.di.contexts.autocomplete'

DI context used during autocomplete invocation.

COMMAND = 'lightbulb.di.contexts.command'

DI context used during command invocation, including for hooks and error handlers.

DEFAULT = 'lightbulb.di.contexts.default'

The base DI context - all other contexts are built with this as the parent.

LISTENER = 'lightbulb.di.contexts.listener'

DI context used during listener invocation.

TASK = 'lightbulb.di.contexts.task'

DI context used during task invocation.

class DefaultContainer

Injectable type representing the dependency container for the default context.

alias of Container

class DependencyInjectionManager[source]

Class which contains dependency injection functionality.

async close() None[source]

Close the default dependency injection context. This must be called if you wish the teardown functions for any dependencies registered for the default registry to be called.

Returns:

None

enter_context(context: Context = 'lightbulb.di.contexts.default', /) AsyncIterator[Container][source]

Context manager that ensures a dependency injection context is available for the nested operations.

Parameters:

context – The context to enter. If a container for the given context already exists, it will be returned and a new container will not be created.

Yields:

Container – The container that has been entered.

Example

# Enter a specific context ('client' is your lightbulb.Client instance)
async with client.di.enter_context(lightbulb.di.Contexts.COMMAND):
    await some_function_that_needs_dependencies()

Note

If you want to enter multiple contexts - i.e. a command context that requires the default context to be available first - you should call this once for each context that is needed.

async with (
    client.di.enter_context(lightbulb.di.Contexts.DEFAULT),
    client.di.enter_context(lightbulb.di.Contexts.COMMAND)
):
    ...

Warning

If you have disabled dependency injection using the LIGHTBULB_DI_DISABLED environment variable, this method will do nothing and the context manager will return None. Most users will never have to worry about this, but it is something to consider. The type-hint does not reflect this to prevent your type-checker complaining about not checking for None.

registry_for(context: Context, /) Registry[source]

Get the dependency registry for the given context. Creates one if necessary.

Parameters:

context – The injection context to get the registry for.

Returns:

The dependency registry for the given context.

property default_container: Container | None

The container being used to provide dependencies for the DEFAULT context. This will be None until the first time any injection context is entered.

class ListenerContainer

Injectable type representing the dependency container for the listener context.

alias of Container

class TaskContainer

Injectable type representing the dependency container for the task context.

alias of Container

with_di(func: AsyncFnT) AsyncFnT[source]
with_di(func: Callable[P, R]) Callable[P, Coroutine[t.Any, t.Any, R]]

Decorator that enables dependency injection on the decorated function. If dependency injection has been disabled globally then this function does nothing and simply returns the object that was passed in.

Parameters:

func – The function to enable dependency injection for.

Returns:

The function with dependency injection enabled, or the same function if DI has been disabled globally.

Warning

Dependency injection relies on a context (note: not a lightbulb Context) being available when the function is called. If the function is called during a lightbulb-controlled flow (such as command invocation or error handling), then one will be available automatically. Otherwise, you will have to set up the context yourself using the helper context manager enter_context().

INJECTED: Final[Any] = <lightbulb.Marker: 'INJECTED'>

Flag value used to explicitly mark that a function parameter should be dependency-injected.

This exists to stop type checkers complaining that function arguments are not provided when calling dependency-injection enabled functions.

Example

@lightbulb.di.with_di
async def foo(bar: SomeClass = lightbulb.INJECTED) -> None:
    ...

# Type-checker shouldn't error that a parameter is missing
await foo()