dependency_injector.containers

Containers module.

class dependency_injector.containers.Container

Bases: object

Abstract container.

class dependency_injector.containers.DeclarativeContainer(**overriding_providers)

Bases: Container

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 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: 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]

from_json_schema(filepath)

Build container providers from JSON schema.

from_schema(schema)

Build container providers from schema.

from_yaml_schema(filepath, loader=None)

Build container providers from YAML schema.

You can specify type of loader as a second argument. By default, method uses SafeLoader.

init_resources()

Initialize all container resources.

is_auto_wiring_enabled()

Check if auto wiring is needed.

load_config()

Load configuration.

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[object, 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, from_package=None)

Wire container providers with provided packages and modules.

Return type:

None

class dependency_injector.containers.WiringConfiguration(modules=None, packages=None, from_package=None, auto_wire=True)

Bases: object

Container wiring configuration.

dependency_injector.containers.copy(base_container)

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:

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

Returns:

Declarative container copying decorator.

Return type:

callable(DeclarativeContainer)

dependency_injector.containers.is_container(instance)

Check if instance is container instance.

Parameters:

instance (object) – Instance to be checked.

Return type:

bool

dependency_injector.containers.override(container)

DeclarativeContainer overriding decorator.

Parameters:

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

Returns:

Declarative container overriding decorator.

Return type:

callable(DeclarativeContainer)