dependency_injector.containers

Containers module.

class dependency_injector.containers.DeclarativeContainer(**overriding_providers)

Bases: object

Declarative inversion of control container.

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

alias of 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

classmethod resolve_provider_name(provider_to_resolve)

Try to resolve provider name by its instance.

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

Bases: type

Declarative inversion of control container meta class.

mro()

Return a type’s method resolution order.

class dependency_injector.containers.DynamicContainer

Bases: object

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

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

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

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.

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 copy 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)