dependency_injector.providers

Dependency injector providers.

Powered by Cython.

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 postional 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
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 postional 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 postional 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
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 postional 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__ postional 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__ postional 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
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 __init__ postional 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
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.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
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 postional 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
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 postional 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
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.Configuration

Bases: dependency_injector.providers.Object

Configuration provider.

Configuration provider helps with implementing late static binding of configuration options - use first, define later.

config = Configuration('config')

print(config.section1.option1())  # None
print(config.section1.option2())  # None

config.override({'section1': {'option1': 1,
                              'option2': 2}})

print(config.section1.option1())  # 1
print(config.section1.option2())  # 2
delegate()

Return provider’s delegate.

Return type:Delegate
get_name()

Name of configuration unit.

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
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.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 postional 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
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 postional 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
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.Delegate

Bases: dependency_injector.providers.Object

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
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.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 postional 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
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 postional 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 postional 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
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 postional 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__ postional 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__ postional 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
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 __init__ postional 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
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
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
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
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-determenistic 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_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.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-determenistic 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_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__ postional 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__ postional 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
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 __init__ postional 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
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
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.Injection

Bases: object

Abstract injection class.

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
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 implemeting 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.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
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.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
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
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.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
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
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
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.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