Dependency Injection Framework¶
Main class for injection tools
- class autoinject.injection.InjectionManager(include_entry_points: bool = True)[source]¶
Responsible for managing the class registry, context manager, and providing dependency injection tools.
The main instance of this is provided as part of the
autoinjectlibrary namedinjector. Users should make use of that instance instead of creating their own.The primary way to register new classes for injection is using the
autoinject.injection.InjectionManager.injectable()decorator. This registers the class with the class registry and is suitable for classes that have a constructor with no arguments other than injectable ones. More complex classes should use theautoinject.injection.InjectionManager.register()decorator and provide suitable arguments to support class construction.Dependencies can be injected in two fashions: as part of the arguments to a function or method, or as object attributes when
__init__()is called. For the former, use theautoinject.injection.InjectionManager.inject()decorator; it will automatically provide an appropriate instance of the objects based on the type-hint of the parameter. For the latter, use theautoinject.injection.InjectionManager.construct()decorator on the class’s__init__()method. It will search for CLASS attributes with an injectable type-hint and inject the objects into the INSTANCE attributes as required.- ContextVars(context: ContextVarManager | Literal['_default', 'same', 'empty', 'copy'] | ContextProtocol | Context | None = None, suppress_exit_warning: bool = False) ContextVarManager[source]¶
Use as a context manager for managing an area where all context_cache injectables are the same.
- as_thread_run(fn: Callable[[P], Q] | _InjectWrapper) Callable[source]¶
Decorate a threading.Thread.run() method to ensure its context variables are cleaned up.
- construct(func: Callable[[P], None]) Callable[[P], None][source]¶
- construct(func: Type[Q]) Type[Q]
Method decorator for
__init__()that will inspect the class attributes for those with a type-hint that is injectable and then inject those dependencies into the corresponding instance attribute.class MyInjectedClass: injected_attribute: MyClass = None @injector.construct def __init__(self): pass
- cv_cleanup(context: Context | None = None)[source]¶
Clean up the cache for the current contextvars context or the given one
- cv_freshen(context: Context | None = None) Token[source]¶
Freshen the context var to get a new context and return the old one
- cv_restore(token: Token, context: Context | None = None)[source]¶
Restore the context var to what it was
- cv_touch(context: Context | None = None)[source]¶
Touch the contextvar for autoinjector to ensure it exists.
- get(cls_name: Type[Q]) Q[source]¶
- get(cls_name: str) Any
Wrapper around
autoinject.context_manager.ContextManager.get_object()
- inject(*, with_contextvars: bool = False, context_mode: ContextVarManager | Literal['_default', 'same', 'empty', 'copy'] | ContextProtocol | Context | None = '_default', as_thread_run: bool = False, suppress_exit_warning: bool = False) Callable[source]¶
- inject(func: Callable[[P], Q] | _InjectWrapper, *, with_contextvars: bool = False, context_mode: ContextVarManager | Literal['_default', 'same', 'empty', 'copy'] | ContextProtocol | Context | None = '_default', as_thread_run: bool = False, suppress_exit_warning: bool = False) Callable
Function or method decorator responsible for injecting dependencies into the argument list. A dependency is defined as a parameter with a type-hint that has been registered. To make sure your IDE code-completion works properly, it is recommended to place these at the end of the argument list and to give them a default value of None.
@injector.inject def my_function(some_param, injected_param: MyClass = None): pass
- injectable()[source]¶
Class decorator for basic registration of injectable objects that don’t require external input.
@injector.injectable class MyClass: def __init__(self): # Cannot have any required arguments pass
- injectable_global()[source]¶
Class decorator for basic registration of injectable objects that don’t require external input, but with a global scope.
- injectable_nocache()[source]¶
Class decorator for basic registration of injectable objects that don’t require external input, but with no caching.
- override(cls_name: Type[Q], new_constructor: Callable[[...], Q] | type | None, *args, **kwargs)[source]¶
- override(cls_name: str, new_constructor: Callable[[...], Any] | type | None, *args, **kwargs)
Override one class with another.
- register(cls_name: Type[Q], *args, **kwargs) Callable[[Type[Q]], Type[Q]][source]¶
- register(cls_name: str, *args, **kwargs) Callable[[...], Any]
- register(cls_name: None = None, *args, **kwargs) Callable[[...], Any]
- Decorator for advanced registration of injectable objects. Includes support for passing positional and
keyword arguments to the constructor, and for specifying an alternative constructor method
@injector.register("test.MyClass", "one") class MyClass: def __init__(self, param_one): pass # alternatively, using a function to build the object @injector.register("test.MyClass", "one") def _build_my_class(param_one): return MyClass(param_one)
- Parameters:
cls_name (str or type) – The name of the class being registered
args – Positional arguments for the constructor
kwargs – Keyword arguments for the constructor
- register_constructor(cls_name: Type[Q], constructor: Callable[[...], Q] | type | str | None, *args, **kwargs)[source]¶
- register_constructor(cls_name: str, constructor: Callable[[...], Any] | type | str | None, *args, **kwargs)
Wrapper around
autoinject.class_registry.ClassRegistry.register_class()
- register_informant(context_informant: SituationInformant)[source]¶
Wrapper around
autoinject.context_manager.ContextManager.register_informant()
- test_case(fixtures_or_fn: Callable[[P], Q] | _InjectWrapper) Callable[source]¶
- test_case(fixtures_or_fn: Type[Q]) Type[Q]
- test_case(fixtures_or_fn: Dict[type | str, Callable[[], Any] | type] | None = None) Callable
Decorate a test case to get a separate global context and to provide fixtures.
- with_contextvars(obj: Callable[[P], Q] | _InjectWrapper) Callable[source]¶
- with_contextvars(*, context_mode: Literal['_default', 'same', 'empty', 'copy'] | ContextProtocol | Context | None | ContextVarManager, suppress_exit_warning: bool = False) Callable
Decorate a function to give it a new contextvars context (see .ContextVars) and cleanup after.
- with_empty_contextvars(fn: Callable[[P], Q] | _InjectWrapper) Callable[source]¶
Create a new empty context to run this in