dependency_injector.providers

Providers module.

class dependency_injector.providers.AbstractCallable

Bases: dependency_injector.providers.Callable

Abstract callable provider.

AbstractCallable is a Callable provider that must be explicitly overridden before calling.

Overriding of AbstractCallable is possible only by another Callable provider.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.AbstractCoroutine

Bases: dependency_injector.providers.Coroutine

Abstract coroutine provider.

AbstractCoroutine is a Coroutine provider that must be explicitly overridden before calling.

Overriding of AbstractCoroutine is possible only by another Coroutine provider.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.AbstractFactory

Bases: dependency_injector.providers.Factory

Abstract factory provider.

AbstractFactory is a Factory provider that must be explicitly overridden before calling.

Overriding of AbstractFactory is possible only by another Factory provider.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provided type.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.AbstractSingleton

Bases: dependency_injector.providers.BaseSingleton

Abstract singleton provider.

AbstractSingleton is a Singleton provider that must be explicitly overridden before calling.

Overriding of AbstractSingleton is possible only by another BaseSingleton provider.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.AttributeGetter

Bases: dependency_injector.providers.Provider

Provider that returns the attribute of the injected instance.

You should not create this provider directly. See ProvidedInstance instead.

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

name

Return name of the attribute.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.BaseSingleton

Bases: dependency_injector.providers.Provider

Base class of singleton providers.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.Callable

Bases: dependency_injector.providers.Provider

Callable provider calls wrapped callable on every call.

Callable supports positional and keyword argument injections:

some_function = Callable(some_function,
                         'positional_arg1', 'positional_arg2',
                         keyword_argument1=3, keyword_argument=4)

# or

some_function = Callable(some_function) \
    .add_args('positional_arg1', 'positional_arg2') \
    .add_kwargs(keyword_argument1=3, keyword_argument=4)

# or

some_function = Callable(some_function)
some_function.add_args('positional_arg1', 'positional_arg2')
some_function.add_kwargs(keyword_argument1=3, keyword_argument=4)
add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.CallableDelegate

Bases: dependency_injector.providers.Delegate

Callable delegate injects delegating callable “as is”.

provides

Value that have to be provided.

Type

object

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Configuration

Bases: dependency_injector.providers.Object

Configuration provider provides configuration options to the other providers.

config = Configuration('config')
print(config.section1.option1())  # None
print(config.section1.option2())  # None
config.from_dict(
    {
        'section1': {
            'option1': 1,
            'option2': 2,
        },
    },
)
print(config.section1.option1())  # 1
print(config.section1.option2())  # 2
delegate()

Return provider’s delegate.

Return type

Delegate

from_dict()

Load configuration from the dictionary.

Loaded configuration is merged recursively over existing configuration.

Parameters

options (dict) – Configuration options.

Return type

None

from_env()

Load configuration value from the environment variable.

Parameters
  • name (str) – Name of the environment variable.

  • default (str) – Default value that is used if environment variable does not exist.

Return type

None

from_ini()

Load configuration from the ini file.

Loaded configuration is merged recursively over existing configuration.

Parameters

filepath (str) – Path to the configuration file.

Return type

None

from_yaml()

Load configuration from the yaml file.

Loaded configuration is merged recursively over existing configuration.

Parameters

filepath (str) – Path to the configuration file.

Return type

None

get()

Return configuration option.

Parameters

selector (str) – Selector string, e.g. “option1.option2”

Returns

Option value.

Return type

Any

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_cache()

Reset children providers cache.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set()

Override configuration option.

Parameters
  • selector (str) – Selector string, e.g. “option1.option2”

  • value (Any) – Overriding value

Returns

Overriding context.

Return type

OverridingContext

update()

Set configuration options.

Deprecated since version 3.11: Use Configuration.override() instead.

Parameters

value (object | dict) – Value of configuration option.

Return type

None

class dependency_injector.providers.ConfigurationOption

Bases: dependency_injector.providers.Provider

Child configuration option provider.

This provider should not be used directly. It is a part of the Configuration provider.

delegate()

Return provider’s delegate.

Return type

Delegate

from_dict()

Load configuration from the dictionary.

Loaded configuration is merged recursively over existing configuration.

Parameters

options (dict) – Configuration options.

Return type

None

from_env()

Load configuration value from the environment variable.

Parameters
  • name (str) – Name of the environment variable.

  • default (str) – Default value that is used if environment variable does not exist.

Return type

None

from_ini()

Load configuration from the ini file.

Loaded configuration is merged recursively over existing configuration.

Parameters

filepath (str) – Path to the configuration file.

Return type

None

from_yaml()

Load configuration from the yaml file.

Loaded configuration is merged recursively over existing configuration.

Parameters

filepath (str) – Path to the configuration file.

Return type

None

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

update()

Set configuration options.

Deprecated since version 3.11: Use Configuration.override() instead.

Parameters

value (object | dict) – Value of configuration option.

Return type

None

class dependency_injector.providers.Container

Bases: dependency_injector.providers.Provider

Container provider provides an instance of declarative container.

Warning

Provider is experimental. Its interface may change.

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Coroutine

Bases: dependency_injector.providers.Callable

Coroutine provider creates wrapped coroutine on every call.

Coroutine supports positional and keyword argument injections:

some_coroutine = Coroutine(some_coroutine,
                           'positional_arg1', 'positional_arg2',
                           keyword_argument1=3, keyword_argument=4)

# or

some_coroutine = Coroutine(some_coroutine) \
    .add_args('positional_arg1', 'positional_arg2') \
    .add_kwargs(keyword_argument1=3, keyword_argument=4)

# or

some_coroutine = Coroutine(some_coroutine)
some_coroutine.add_args('positional_arg1', 'positional_arg2')
some_coroutine.add_kwargs(keyword_argument1=3, keyword_argument=4)
add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.CoroutineDelegate

Bases: dependency_injector.providers.Delegate

Coroutine delegate injects delegating coroutine “as is”.

provides

Value that have to be provided.

Type

object

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Delegate

Bases: dependency_injector.providers.Provider

Delegate provider returns provider “as is”.

provides

Value that have to be provided.

Type

object

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.DelegatedCallable

Bases: dependency_injector.providers.Callable

Callable that is injected “as is”.

DelegatedCallable is a Callable, that is injected “as is”.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.DelegatedCoroutine

Bases: dependency_injector.providers.Coroutine

Coroutine provider that is injected “as is”.

DelegatedCoroutine is a Coroutine, that is injected “as is”.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.DelegatedFactory

Bases: dependency_injector.providers.Factory

Factory that is injected “as is”.

provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provided type.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.DelegatedSingleton

Bases: dependency_injector.providers.Singleton

Delegated singleton is injected “as is”.

provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.DelegatedThreadLocalSingleton

Bases: dependency_injector.providers.ThreadLocalSingleton

Delegated thread-local singleton is injected “as is”.

provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.DelegatedThreadSafeSingleton

Bases: dependency_injector.providers.ThreadSafeSingleton

Delegated thread-safe singleton is injected “as is”.

provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.DependenciesContainer

Bases: dependency_injector.providers.Object

DependenciesContainer provider provides set of dependencies.

Dependencies container provider is used to implement late static binding for a set of providers of a particular container.

Example code:

class Adapters(containers.DeclarativeContainer):
    email_sender = providers.Singleton(SmtpEmailSender)

class TestAdapters(containers.DeclarativeContainer):
    email_sender = providers.Singleton(EchoEmailSender)

class UseCases(containers.DeclarativeContainer):
    adapters = providers.DependenciesContainer()

    signup = providers.Factory(SignupUseCase,
                               email_sender=adapters.email_sender)

use_cases = UseCases(adapters=Adapters)
# or
use_cases = UseCases(adapters=TestAdapters)

# Another file
from .containers import use_cases

use_case = use_cases.signup()
use_case.execute()
delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

providers

Read-only dictionary of dependency providers.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Dependency

Bases: dependency_injector.providers.Provider

Dependency provider describes dependency interface.

This provider is used for description of dependency interface. That might be useful when dependency could be provided in the client’s code only, but it’s interface is known. Such situations could happen when required dependency has non-deterministic list of dependencies itself.

database_provider = Dependency(sqlite3.dbapi2.Connection)
database_provider.override(Factory(sqlite3.connect, ':memory:'))

database = database_provider()
instance_of

Class of required dependency.

type

type

delegate()

Return provider’s delegate.

Return type

Delegate

instance_of

Return class of required dependency.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provided_by()

Set external dependency provider.

Parameters

provider (Provider) – Provider that provides required dependency.

Return type

None

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Dict

Bases: dependency_injector.providers.Provider

Dict provider provides a dictionary of values.

Dict provider is needed for injecting a dictionary of dependencies. It handles keyword argument injections the same way as Factory provider.

Positional argument injections are not supported.

dispatcher_factory = Factory(
    Dispatcher,
    modules=Dict(
        module1=Factory(ModuleA, dependency_a),
        module2=Factory(ModuleB, dependency_b),
    ),
)

dispatcher = dispatcher_factory()

# is equivalent to:

dispatcher = Dispatcher(
    modules={
        'module1': ModuleA(dependency_a),
        'module2': ModuleB(dependency_b),
    },
)
add_kwargs()

Add keyword argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.EnvInterpolation

Bases: configparser.BasicInterpolation

Interpolation which expands environment variables in values.

class dependency_injector.providers.ExternalDependency

Bases: dependency_injector.providers.Dependency

ExternalDependency provider describes dependency interface.

This provider is used for description of dependency interface. That might be useful when dependency could be provided in the client’s code only, but it’s interface is known. Such situations could happen when required dependency has non-deterministic list of dependencies itself.

database_provider = ExternalDependency(sqlite3.dbapi2.Connection)
database_provider.override(Factory(sqlite3.connect, ':memory:'))

database = database_provider()

Deprecated since version 3.9: Use Dependency instead.

instance_of

Class of required dependency.

type

type

delegate()

Return provider’s delegate.

Return type

Delegate

instance_of

Return class of required dependency.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provided_by()

Set external dependency provider.

Parameters

provider (Provider) – Provider that provides required dependency.

Return type

None

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Factory

Bases: dependency_injector.providers.Provider

Factory provider creates new instance on every call.

Factory supports positional & keyword argument injections, as well as attribute injections.

Positional and keyword argument injections could be defined like this:

factory = Factory(SomeClass,
                  'positional_arg1', 'positional_arg2',
                  keyword_argument1=3, keyword_argument=4)

# or

factory = Factory(SomeClass) \
    .add_args('positional_arg1', 'positional_arg2') \
    .add_kwargs(keyword_argument1=3, keyword_argument=4)

# or

factory = Factory(SomeClass)
factory.add_args('positional_arg1', 'positional_arg2')
factory.add_kwargs(keyword_argument1=3, keyword_argument=4)

Attribute injections are defined by using Factory.add_attributes():

factory = Factory(SomeClass) \
    .add_attributes(attribute1=1, attribute2=2)

Retrieving of provided instance can be performed via calling Factory object:

factory = Factory(SomeClass)
some_object = factory()
provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provided type.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.FactoryAggregate

Bases: dependency_injector.providers.Provider

Factory providers aggregate.

FactoryAggregate is an aggregate of Factory providers.

FactoryAggregate is a delegated provider, meaning that it is injected “as is”.

All aggregated factories could be retrieved as a read-only dictionary FactoryAggregate.factories or just as an attribute of FactoryAggregate.

delegate()

Return provider’s delegate.

Return type

Delegate

factories

Return dictionary of factories, read-only.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.FactoryDelegate

Bases: dependency_injector.providers.Delegate

Factory delegate injects delegating factory “as is”.

provides

Value that have to be provided.

Type

object

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Injection

Bases: object

Abstract injection class.

class dependency_injector.providers.ItemGetter

Bases: dependency_injector.providers.Provider

Provider that returns the item of the injected instance.

You should not create this provider directly. See ProvidedInstance instead.

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

name

Return name of the item.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.List

Bases: dependency_injector.providers.Provider

List provider provides a list of values.

List provider is needed for injecting a list of dependencies. It handles positional argument injections the same way as Factory provider.

Keyword argument injections are not supported.

dispatcher_factory = Factory(
    Dispatcher,
    modules=List(
        Factory(ModuleA, dependency_a),
        Factory(ModuleB, dependency_b),
    ),
)

dispatcher = dispatcher_factory()

# is equivalent to:

dispatcher = Dispatcher(
    modules=[
        ModuleA(dependency_a),
        ModuleB(dependency_b),
    ],
)
add_args()

Add positional argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.MethodCaller

Bases: dependency_injector.providers.Provider

Provider that calls the method of the injected instance.

You should not create this provider directly. See ProvidedInstance instead.

args

Return positional argument injections.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.NamedInjection

Bases: dependency_injector.providers.Injection

Keyword injection class.

get_name()

Return injection value.

get_original_value()

Return original value.

get_value()

Return injection value.

class dependency_injector.providers.Object

Bases: dependency_injector.providers.Provider

Object provider returns provided instance “as is”.

provides

Value that have to be provided.

Type

object

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.OverridingContext

Bases: object

Provider overriding context.

OverridingContext is used by Provider.override() for implementing with contexts. When OverridingContext is closed, overriding that was created in this context is dropped also.

with provider.override(another_provider):
    assert provider.overridden
assert not provider.overridden
class dependency_injector.providers.PositionalInjection

Bases: dependency_injector.providers.Injection

Positional injection class.

get_original_value()

Return original value.

get_value()

Return injection value.

class dependency_injector.providers.ProvidedInstance

Bases: dependency_injector.providers.Provider

Provider that helps to inject attributes and items of the injected instance.

You can use it like that:

service = providers.Singleton(Service)

client_factory = providers.Factory(
    Client,
    value1=service.provided[0],
    value2=service.provided.value,
    value3=service.provided.values[0],
    value4=service.provided.get_value.call(),
)

You should not create this provider directly. Get it from the .provided attribute of the injected provider. This attribute returns the ProvidedInstance for that provider.

Providers that have .provided attribute:

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Provider

Bases: object

Base provider class.

Provider is callable (implements __call__ method). Every call to provider object returns provided result, according to the providing strategy of particular provider. This callable functionality is a regular part of providers API and it should be the same for all provider’s subclasses.

Implementation of particular providing strategy should be done in Provider._provide() of Provider subclass. Current method is called every time when not overridden provider is called.

Provider implements provider overriding logic that should be also common for all providers:

provider1 = Factory(SomeClass)
provider2 = Factory(ChildSomeClass)

provider1.override(provider2)

some_instance = provider1()
assert isinstance(some_instance, ChildSomeClass)

Also Provider implements helper function for creating its delegates:

provider = Factory(object)
delegate = provider.delegate()

delegated = delegate()

assert provider is delegated

All providers should extend this class.

overridden

Tuple of overriding providers, if any.

type

tuple[Provider] | None

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Resource

Bases: dependency_injector.providers.Provider

Resource provider provides a component with initialization and shutdown.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

init()

Initialize resource.

initialized

Check if resource is initialized.

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

shutdown()

Shutdown resource.

class dependency_injector.providers.Selector

Bases: dependency_injector.providers.Provider

Selector provider selects provider based on the configuration value or other callable.

Selector provider has a callable called selector and a dictionary of providers.

The selector callable is provided as a first positional argument. It can be Configuration provider or any other callable. It has to return a string value. That value is used as a key for selecting the provider from the dictionary of providers.

The providers are provided as keyword arguments. Argument name is used as a key for selecting the provider.

config = Configuration()

selector = Selector(
    config.one_or_another,
    one=providers.Factory(SomeClass),
    another=providers.Factory(SomeOtherClass),
)

config.override({'one_or_another': 'one'})
instance_1 = selector()
assert isinstance(instance_1, SomeClass)

config.override({'one_or_another': 'another'})
instance_2 = selector()
assert isinstance(instance_2, SomeOtherClass)
delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

providers

Return providers.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.Singleton

Bases: dependency_injector.providers.BaseSingleton

Singleton provider returns same instance on every call.

Singleton provider creates instance once and returns it on every call. Singleton extends Factory, so, please follow Factory documentation for getting familiar with injections syntax.

Retrieving of provided instance can be performed via calling Singleton object:

singleton = Singleton(SomeClass)
some_object = singleton()
provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.SingletonDelegate

Bases: dependency_injector.providers.Delegate

Singleton delegate injects delegating singleton “as is”.

provides

Value that have to be provided.

Type

object

delegate()

Return provider’s delegate.

Return type

Delegate

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return provider.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

class dependency_injector.providers.ThreadLocalSingleton

Bases: dependency_injector.providers.BaseSingleton

Thread-local singleton provides single objects in scope of thread.

provided_type

If provided type is defined, provider checks that providing class is its subclass.

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.ThreadSafeSingleton

Bases: dependency_injector.providers.BaseSingleton

Thread-safe singleton provider.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

attributes

Return attribute injections.

clear_args()

Drop __init__ positional argument injections.

Returns

Reference self

clear_attributes()

Drop attribute injections.

Returns

Reference self

clear_kwargs()

Drop __init__ keyword argument injections.

Returns

Reference self

cls

Return provided type.

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

reset()

Reset cached instance, if any.

Return type

None

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

class dependency_injector.providers.TypedConfigurationOption

Bases: dependency_injector.providers.Callable

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

clear_args()

Drop positional argument injections.

Returns

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns

Reference self

delegate()

Return provider’s delegate.

Return type

Delegate

kwargs

Return keyword argument injections.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override()

Override provider with another provider.

Parameters

provider (Provider) – Overriding provider.

Raise

dependency_injector.errors.Error

Returns

Overriding context.

Return type

OverridingContext

provided

Return ProvidedInstance provider.

provider

Return provider’s delegate.

Return type

Delegate

provides

Return wrapped callable.

reset_last_overriding()

Reset last overriding provider.

Raise

dependency_injector.errors.Error if provider is not overridden.

Return type

None

reset_override()

Reset all overriding providers.

Return type

None

set_args()

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

dependency_injector.providers.deepcopy()

Return full copy of provider or container with providers.

dependency_injector.providers.ensure_is_provider()

Check if instance is provider instance and return it.

Parameters

instance (object) – Instance to be checked.

Raise

dependency_injector.errors.Error if provided instance is not provider.

Return type

dependency_injector.providers.Provider

dependency_injector.providers.is_delegated()

Check if instance is delegated provider.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.providers.is_provider()

Check if instance is provider instance.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.providers.merge_dicts()

Merge dictionaries recursively.

Parameters
  • dict1 (dict) – Dictionary 1

  • dict2 (dict) – Dictionary 2

Returns

New resulting dictionary

Return type

dict

dependency_injector.providers.parse_named_injections()

Parse named injections.

dependency_injector.providers.parse_positional_injections()

Parse positional injections.

dependency_injector.providers.represent_provider()

Return string representation of provider.

Parameters
  • provider (object) – Provider object

  • provides – Object that provider provides

Returns

String representation of provider

Return type

str

dependency_injector.providers.yaml_env_marker_constructor()

“Replace environment variable marker with its value.