component_injector

class component_injector.Injector[source]

Provides a basic injector namespace. It’s common to use one injector per project.

get_component(type_: Type[T]) → T[source]

Get a component from the injector’s current scope. Materialize it using a factory if necessary.

Note that it is an error to use this function to get a component that has a factory that returns an Awaitable.

Parameters:type – The type of the component to return.
Returns:The materialized component.
get_component_async(type_: Type[T]) → T[source]

Get a component from the injector’s current scope. Materialize it using a factory if necessary.

Use this method if the component’s factory function returns an Awaitable.

Parameters:type – The type of the component to return.
Returns:The materialized component.
inject(f: Callable[[...], T]) → Callable[[...], T][source]

This decorator will connect the injector to a function or method. When the resulting function is called, the provided arguments will be checked against the function’s signature and any missing arguments the injector has a component or factory those arguments will be filled in.

Parameters:f – The function or method to inject components into.
Returns:The decorated function.
register(component: Any, *, bases: bool = True, overwrite_bases: bool = True) → None[source]

Register a new component with the injector.

Parameters:
  • component – The component to register with the injector.
  • bases – Besides registering the exact component type, also register for all of the component’s base classes. Defaults to True.
  • overwrite_bases – If any of the component’s base classes are already registered with the injector, overwrite those registrations. Defaults to True.
register_factory(factory: Callable[[], Any], *, bases: bool = True, overwrite_bases: bool = True, persistent: bool = False) → None[source]

Register a new factory function with the injector. Not that the factory function’s return type annotation should be set to the type of the component you want to inject.

Parameters:
  • factory – The factory function. Will be called without arguments and should return the instantiated component. If the factory returns an Awaitable it can only used to inject into coroutine functions.
  • bases – Besides registering the exact component type, also register for all of the component’s base classes. Defaults to True.
  • overwrite_bases – If any of the component’s base classes are already registered with the injector, overwrite those registrations. Defaults to True.
  • persistent – When materializing the component using the factory, insert the component into the scope where the factory is registered instead of the current scope. Defaults to False.
scope() → component_injector.Context[source]

Return a context manager that you can use to enter a new scpoe. When leaving the scope, any components or factories added to the injector will be forgotten.

Returns:The scope context object. You can use this to re-enter this scope at a later time if needed.