lightbulb.internal.di¶
- class lightbulb.internal.di.DependencyInjectionManager[source]¶
Class which contains dependency injection functionality - intended to be used with composition.
- register_dependency(type_: type[T], factory: t.Callable[[], MaybeAwaitable[T]], *, enter: bool = False) None [source]¶
Register a dependency as usable by dependency injection. All dependencies are considered to be singletons, meaning the factory will always be called at most once.
- property di_container: Container¶
The dependency injection container used for this instance. Lazily instantiated.
- property di_registry: Registry¶
The dependency injection registry containing dependencies available for this instance.
- class lightbulb.internal.di.LazyInjecting(func: Callable[[...], Awaitable[Any]], self_: Any = None)[source]¶
Wrapper for a callable that implements dependency injection. When called, resolves the required dependencies and calls the original callable. Only supports asynchronous functions.
You should generally never have to instantiate this yourself - you should instead use one of the decorators that applies this to the target automatically.
- lightbulb.internal.di.ensure_di_context(client: DependencyInjectionManager) Generator[None, Any, Any] [source]¶
Context manager that ensures a dependency injection context is available for the nested operations.
- Parameters:
client (
DependencyInjectionAware
) – The client that is aware of the required information to be able to supply dependencies for injection.
Example
with lightbulb.ensure_di_context(client): await some_function_that_needs_dependencies()
- lightbulb.internal.di.find_injectable_kwargs(func: Callable[[...], Any], passed_args: int, passed_kwargs: Collection[str]) dict[str, Any] [source]¶
Given a function, parse the signature to discover which parameters are suitable for dependency injection.
A parameter is suitable for dependency injection if:
It has a type annotation
It has no default value
It is not positional-only (injected parameters are always passed as a keyword argument)
- Parameters:
func – The function to discover the dependency injection suitable parameters for.
passed_args (
int
) – The number of positional arguments passed to the function in this invocation.passed_kwargs (
Collection
[str
]) – The names of all the keyword arguments passed to the function in this invocation.
- Returns:
- Mapping of parameter name to parameter annotation
value for parameters that are suitable for dependency injection.
- Return type:
- lightbulb.internal.di.with_di(func: AnyAsyncCallableT) AnyAsyncCallableT [source]¶
Enables dependency injection on the decorated asynchronous 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 asynchronous 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 managersetup_di_context
.