dependency_injector.containers

Containers module.

class dependency_injector.containers.Container

Bases: object

Abstract container.

class dependency_injector.containers.DeclarativeContainer(**overriding_providers)

Bases: dependency_injector.containers.Container

Declarative inversion of control container.

class Services(DeclarativeContainer):
    auth = providers.Factory(AuthService)
    users = providers.Factory(UsersService,
                              auth_service=auth)
instance_type

alias of dependency_injector.containers.DynamicContainer

classmethod override(overriding)

Override current container by overriding container.

Parameters

overriding (DeclarativeContainer) – Overriding container.

Raise

dependency_injector.errors.Error if trying to override container by itself or its subclasses

Return type

None

provider_type

alias of dependency_injector.providers.Provider

classmethod reset_last_overriding()

Reset last overriding provider for each container providers.

Return type

None

classmethod reset_override()

Reset all overridings for each container providers.

Return type

None

class dependency_injector.containers.DeclarativeContainerMetaClass(class_name, bases, attributes)

Bases: type

Declarative inversion of control container meta class.

property dependencies

Return dependency providers dictionary.

Dependency providers can be both of dependency_injector.providers.Dependency and dependency_injector.providers.DependenciesContainer.

Return type

dict[str, dependency_injector.providers.Provider]

mro()

Return a type’s method resolution order.

property parent_name

Return parent name.

resolve_provider_name(provider)

Try to resolve provider name.

traverse(types=None)

Return providers traversal generator.

class dependency_injector.containers.DynamicContainer

Bases: dependency_injector.containers.Container

Dynamic inversion of control container.

services = DynamicContainer()
services.auth = providers.Factory(AuthService)
services.users = providers.Factory(UsersService,
                                   auth_service=services.auth)
providers

Read-only dictionary of all providers.

Type

dict[str, dependency_injector.providers.Provider]

overridden

Tuple of overriding containers.

Type

tuple[DynamicContainer]

provider_type

Type of providers that could be placed in container.

Type

type

apply_container_providers_overridings()

Apply container providers’ overridings.

assign_parent(parent)

Assign parent.

check_dependencies()

Check if container dependencies are defined.

If any dependency is undefined, raises an error.

property dependencies

Return dependency providers dictionary.

Dependency providers can be both of dependency_injector.providers.Dependency and dependency_injector.providers.DependenciesContainer.

Return type

dict[str, dependency_injector.providers.Provider]

init_resources()

Initialize all container resources.

override(overriding)

Override current container by overriding container.

Parameters

overriding (DynamicContainer) – Overriding container.

Raise

dependency_injector.errors.Error if trying to override container by itself

Return type

None

override_providers(**overriding_providers)

Override container providers.

Parameters

overriding_providers (dict[str, dependency_injector.providers.Provider]) – Dictionary of providers

Return type

None

property parent_name

Return parent name.

reset_last_overriding()

Reset last overriding provider for each container providers.

Return type

None

reset_override()

Reset all overridings for each container providers.

Return type

None

reset_singletons()

Reset container singletons.

resolve_provider_name(provider)

Try to resolve provider name.

set_provider(name, provider)

Set container provider.

Parameters
Return type

None

set_providers(**providers)

Set container providers.

Parameters

providers (dict[str, dependency_injector.providers.Provider]) – Dictionary of providers

Return type

None

shutdown_resources()

Shutdown all container resources.

traverse(types=None)

Return providers traversal generator.

unwire()

Unwire container providers from previously wired packages and modules.

wire(modules=None, packages=None)

Wire container providers with provided packages and modules.

Return type

None

dependency_injector.containers.copy()

DeclarativeContainer copying decorator.

This decorator copies all providers from provided container to decorated one. If one of the decorated container providers matches to source container providers by name, it would be replaced by reference.

Parameters

container (DeclarativeContainer) – Container that should be copied by decorated container.

Returns

Declarative container’s copying decorator.

Return type

callable(DeclarativeContainer)

dependency_injector.containers.is_container()

Check if instance is container instance.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.containers.override()

DeclarativeContainer overriding decorator.

Parameters

container (DeclarativeContainer) – Container that should be overridden by decorated container.

Returns

Declarative container’s overriding decorator.

Return type

callable(DeclarativeContainer)