dependency_injector.providers

Providers module.

class dependency_injector.providers.AbstractCallable

Bases: 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(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.AbstractCoroutine

Bases: 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(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.AbstractFactory

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.AbstractSingleton

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Aggregate

Bases: Provider

Providers aggregate.

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

All aggregated providers can be retrieved as a read-only dictionary Aggregate.providers or as an attribute of Aggregate, e.g. aggregate.provider.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

providers

Return dictionary of providers, read-only.

Alias for .factories attribute.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_providers(provider_dict=None, **provider_kwargs)

Set providers.

Alias for .set_factories() method.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.AttributeGetter

Bases: Provider

Provider that returns the attribute of the injected instance.

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

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_name(name)

Set name of the attribute.

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.BaseSingleton

Bases: Provider

Base class of singleton providers.

add_args(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Callable

Bases: 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(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.CallableDelegate

Bases: Delegate

Callable delegate injects delegating callable “as is”.

provides

Value that have to be provided.

Type:

object

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Configuration

Bases: 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
async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

from_dict(options, required=<object object>)

Load configuration from the dictionary.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • options (dict) – Configuration options.

  • required (bool) – When required is True, raise an exception if dictionary is empty.

Return type:

None

from_env(name, default=<object object>, required=<object object>, as_=<object object>)

Load configuration value from the environment variable.

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

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

  • required (bool) – When required is True, raise an exception if environment variable is undefined.

  • as (object) – Callable used for type casting (int, float, etc).

Return type:

None

from_ini(filepath, required=<object object>, envs_required=<object object>)

Load configuration from the ini file.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • filepath (str) – Path to the configuration file.

  • required (bool) – When required is True, raise an exception if file does not exist.

  • envs_required (bool) – When True, raises an error on undefined environment variable.

Return type:

None

from_json(filepath, required=<object object>, envs_required=<object object>)

Load configuration from a json file.

Loaded configuration is merged recursively over the existing configuration.

Parameters:
  • filepath (str) – Path to a configuration file.

  • required (bool) – When required is True, raise an exception if file does not exist.

  • envs_required (bool) – When True, raises an exception on undefined environment variable.

Return type:

None

from_pydantic(settings, required=<object object>, **kwargs)

Load configuration from pydantic settings.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • settings (pydantic.BaseSettings (pydantic v1) or pydantic_settings.BaseSettings (pydantic v2 and onwards)) – Pydantic settings instances.

  • required (bool) – When required is True, raise an exception if settings dict is empty.

  • kwargs (Dict[Any, Any]) – Keyword arguments forwarded to pydantic.BaseSettings.dict() call.

Return type:

None

from_value(value)

Load configuration value.

Parameters:

value (object) – Configuration value

Return type:

None

from_yaml(filepath, required=<object object>, loader=None, envs_required=<object object>)

Load configuration from the yaml file.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • filepath (str) – Path to the configuration file.

  • required (bool) – When required is True, raise an exception if file does not exist.

  • loader (yaml.Loader) – YAML loader, YamlLoader is used if not specified.

  • envs_required (bool) – When True, raises an error on undefined environment variable.

Return type:

None

get(selector, required=False)

Return configuration option.

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

  • required (bool) – Required flag, raise error if required option is missing

Returns:

Option value.

Return type:

Any

get_children()

Return children options.

get_default()

Return default.

get_ini_files()

Return list of INI files.

get_json_files()

Return list of JSON files.

get_name()

Return name.

get_pydantic_settings()

Return list of Pydantic settings.

get_strict()

Return strict flag.

get_yaml_files()

Return list of YAML files.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

load(required=<object object>, envs_required=<object object>)

Load configuration.

This method loads configuration from configuration files or pydantic settings that were set earlier with set_*() methods or provided to the __init__(), e.g.:

config = providers.Configuration(yaml_files=[file1, file2])
config.load()
Parameters:
  • required (bool) – When required is True, raise an exception if file does not exist.

  • envs_required (bool) – When True, raises an error on undefined environment variable.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(selector, value)

Override configuration option.

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

  • value (Any) – Overriding value

Returns:

Overriding context.

Return type:

OverridingContext

set_children(children)

Set children options.

set_default(default)

Set default.

set_ini_files(files)

Set list of INI files.

set_json_files(files)

Set list of JSON files.

set_name(name)

Set name.

set_provides(provides)

Set provider provides.

set_pydantic_settings(settings)

Set list of Pydantic settings.

set_strict(strict)

Set strict flag.

set_yaml_files(files)

Set list of YAML files.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

update(value)

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

Child configuration option provider.

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

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

from_dict(options, required=<object object>)

Load configuration from the dictionary.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • options (dict) – Configuration options.

  • required (bool) – When required is True, raise an exception if dictionary is empty.

Return type:

None

from_env(name, default=<object object>, required=<object object>, as_=<object object>)

Load configuration value from the environment variable.

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

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

  • required (bool) – When required is True, raise an exception if environment variable is undefined.

  • as (object) – Callable used for type casting (int, float, etc).

Return type:

None

from_ini(filepath, required=<object object>, envs_required=<object object>)

Load configuration from the ini file.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • filepath (str) – Path to the configuration file.

  • required (bool) – When required is True, raise an exception if file does not exist.

  • envs_required (bool) – When True, raises an error on undefined environment variable.

Return type:

None

from_json(filepath, required=<object object>, envs_required=<object object>)

Load configuration from a json file.

Loaded configuration is merged recursively over the existing configuration.

Parameters:
  • filepath (str) – Path to a configuration file.

  • required (bool) – When required is True, raise an exception if file does not exist.

  • envs_required (bool) – When True, raises an exception on undefined environment variable.

Return type:

None

from_pydantic(settings, required=<object object>, **kwargs)

Load configuration from pydantic settings.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • settings (pydantic.BaseSettings (pydantic v1) or pydantic_settings.BaseSettings (pydantic v2 and onwards)) – Pydantic settings instances.

  • required (bool) – When required is True, raise an exception if settings dict is empty.

  • kwargs (Dict[Any, Any]) – Keyword arguments forwarded to pydantic.BaseSettings.dict() or pydantic_settings.BaseSettings.model_dump() call (based on pydantic version).

Return type:

None

from_value(value)

Load configuration value.

Parameters:

value (object) – Configuration value

Return type:

None

from_yaml(filepath, required=<object object>, loader=None, envs_required=<object object>)

Load configuration from the yaml file.

Loaded configuration is merged recursively over existing configuration.

Parameters:
  • filepath (str) – Path to the configuration file.

  • required (bool) – When required is True, raise an exception if file does not exist.

  • loader (yaml.Loader) – YAML loader, YamlLoader is used if not specified.

  • envs_required (bool) – When True, raises an error on undefined environment variable.

Return type:

None

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(value)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

update(value)

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

Container provider provides an instance of declarative container.

Warning

Provider is experimental. Its interface may change.

apply_overridings()

Apply container overriding.

This method should not be called directly. It is called on declarative container initialization.

assign_parent(parent)

Assign parent.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

overrides

Return providers that are overridden by the current provider.

parent

Return parent.

parent_name

Return parent name.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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

resolve_provider_name(provider)

Try to resolve provider name.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.ContextLocalSingleton

Bases: BaseSingleton

Context-local singleton provides single objects in scope of a context.

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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Coroutine

Bases: 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(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.CoroutineDelegate

Bases: Delegate

Coroutine delegate injects delegating coroutine “as is”.

provides

Value that have to be provided.

Type:

object

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Delegate

Bases: Provider

Delegate provider returns provider “as is”.

provides

Value that have to be provided.

Type:

object

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedCallable

Bases: Callable

Callable that is injected “as is”.

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

add_args(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedCoroutine

Bases: Coroutine

Coroutine provider that is injected “as is”.

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

add_args(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedFactory

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedSingleton

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedThreadLocalSingleton

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedThreadSafeSingleton

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.DependenciesContainer

Bases: 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()
assign_parent(parent)

Assign parent.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

parent

Return parent.

parent_name

Return parent name.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

providers

Read-only dictionary of dependency providers.

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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

resolve_provider_name(provider)

Try to resolve provider name.

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Dependency

Bases: 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 its 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

assign_parent(parent)

Assign parent.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
default

Return default provider.

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

instance_of

Return type.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

is_defined

Return True if dependency is defined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

parent

Return parent.

parent_name

Return parent name.

provided

Return ProvidedInstance provider.

provided_by(provider)

Set external dependency provider.

Parameters:

provider (Provider) – Provider that provides required dependency.

Return type:

None

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_default(default)

Set type.

set_instance_of(instance_of)

Set type.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Dict

Bases: 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(dict_=None, **kwargs)

Add keyword argument injections.

Returns:

Reference self

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(dict_=None, **kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.ExternalDependency

Bases: 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 code only, but its 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

assign_parent(parent)

Assign parent.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
default

Return default provider.

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

instance_of

Return type.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

is_defined

Return True if dependency is defined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

parent

Return parent.

parent_name

Return parent name.

provided

Return ProvidedInstance provider.

provided_by(provider)

Set external dependency provider.

Parameters:

provider (Provider) – Provider that provides required dependency.

Return type:

None

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_default(default)

Set type.

set_instance_of(instance_of)

Set type.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Factory

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.FactoryAggregate

Bases: Aggregate

Factory providers aggregate.

FactoryAggregate is an aggregate of Factory providers.

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

All aggregated providers can be retrieved as a read-only dictionary FactoryAggregate.providers or as an attribute of FactoryAggregate.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

factories

Return dictionary of factories, read-only.

Alias for .providers() attribute.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

providers

Return dictionary of providers, read-only.

Alias for .factories attribute.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_factories(factory_dict=None, **factory_kwargs)

Set factories.

Alias for .set_providers() method.

set_providers(provider_dict=None, **provider_kwargs)

Set providers.

Alias for .set_factories() method.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.FactoryDelegate

Bases: Delegate

Factory delegate injects delegating factory “as is”.

provides

Value that have to be provided.

Type:

object

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Injection

Bases: object

Abstract injection class.

class dependency_injector.providers.ItemGetter

Bases: Provider

Provider that returns the item of the injected instance.

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

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider”s provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_name(name)

Set name of the item.

set_provides(provides)

Set provider”s provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.List

Bases: 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(*args)

Add positional argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.MethodCaller

Bases: 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.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.NamedInjection

Bases: Injection

Keyword injection class.

get_name()

Return injection name.

get_original_value()

Return original value.

get_value()

Return injection value.

set(value)

Set injection.

set_name(name)

Set injection name.

class dependency_injector.providers.Object

Bases: Provider

Object provider returns provided instance “as is”.

provides

Value that have to be provided.

Type:

object

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

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

Positional injection class.

get_original_value()

Return original value.

get_value()

Return injection value.

set(value)

Set injection.

class dependency_injector.providers.ProvidedInstance

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

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

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

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Resource

Bases: Provider

Resource provider provides a component with initialization and shutdown.

add_args(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

init()

Initialize resource.

initialized

Check if resource is initialized.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

shutdown()

Shutdown resource.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Selector

Bases: 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)
async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

providers

Return providers.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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

selector

Return selector.

set_providers(**providers: Provider)

Set providers.

set_selector(selector)

Set selector.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Self

Bases: Provider

Self provider returns own container.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.Singleton

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.SingletonDelegate

Bases: Delegate

Singleton delegate injects delegating singleton “as is”.

provides

Value that have to be provided.

Type:

object

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

last_overriding

Return last overriding provider.

If provider is not overridden, then None is returned.

overridden

Return tuple of overriding providers.

override(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.ThreadLocalSingleton

Bases: 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(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.ThreadSafeSingleton

Bases: BaseSingleton

Thread-safe singleton provider.

add_args(*args)

Add __init__ positional argument injections.

Returns:

Reference self

add_attributes(**kwargs)

Add attribute injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add __init__ keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
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 delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

full_reset()

Reset cached instance in current and all underlying singletons, if any.

Return type:

SingletonFullResetContext

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset()

Reset cached instance, if any.

Return type:

None

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns:

Reference self

set_attributes(**kwargs)

Set attribute injections.

Existing attribute injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.TypedConfigurationOption

Bases: Callable

add_args(*args)

Add positional argument injections.

Returns:

Reference self

add_kwargs(**kwargs)

Add keyword argument injections.

Returns:

Reference self

args

Return positional argument injections.

async_(*args, **kwargs)

Return provided object asynchronously.

This method is a synonym of __call__(). It provides typing stubs for correct type checking with await expression:

database_provider: Provider[DatabaseConnection] = Resource(init_db_async)

async def main():
    db: DatabaseConnection = await database_provider.async_()
    ...
clear_args()

Drop positional argument injections.

Returns:

Reference self

clear_kwargs()

Drop keyword argument injections.

Returns:

Reference self

delegate()

Return provider delegate.

Return type:

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

is_async_mode_disabled()

Check if async mode is disabled.

is_async_mode_enabled()

Check if async mode is enabled.

is_async_mode_undefined()

Check if async mode is undefined.

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(provider)

Override provider with another provider.

Parameters:

provider (Provider) – Overriding provider.

Raise:

dependency_injector.errors.Error

Returns:

Overriding context.

Return type:

OverridingContext

overrides

Return providers that are overridden by the current provider.

provided

Return ProvidedInstance provider.

provider

Return provider”s delegate.

Return type:

Delegate

provides

Return provider provides.

register_overrides(provider)

Register provider that overrides current provider.

related

Return related providers generator.

reset_async_mode()

Reset async mode.

Provider will automatically set the mode on the next call.

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(*args)

Set positional argument injections.

Existing positional argument injections are dropped.

Returns:

Reference self

set_kwargs(**kwargs)

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns:

Reference self

set_provides(provides)

Set provider provides.

traverse(types=None)

Return providers traversal generator.

unregister_overrides(provider)

Unregister provider that overrides current provider.

class dependency_injector.providers.YamlLoader(stream)

Bases: SafeLoader

YAML loader.

This loader mimics yaml.SafeLoader.

check_state_key(key)

Block special attributes/methods from being set in a newly created object, to prevent user-controlled methods from being called during deserialization

dependency_injector.providers.deepcopy(instance, memo=None)

Return full copy of provider or container with providers.

dependency_injector.providers.ensure_is_provider(instance)

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_container_class(instance)

Check if instance is container class.

Parameters:

instance (object) – Instance to be checked.

Return type:

bool

dependency_injector.providers.is_container_instance(instance)

Check if instance is container instance.

Parameters:

instance (object) – Instance to be checked.

Return type:

bool

dependency_injector.providers.is_delegated(instance)

Check if instance is delegated provider.

Parameters:

instance (object) – Instance to be checked.

Return type:

bool

dependency_injector.providers.is_provider(instance)

Check if instance is provider instance.

Parameters:

instance (object) – Instance to be checked.

Return type:

bool

dependency_injector.providers.isasyncgenfunction(obj)

Check if object is an asynchronous generator function.

dependency_injector.providers.isawaitable(obj)

Check if object is a coroutine function.

dependency_injector.providers.iscoroutinefunction(obj)

Check if object is a coroutine function.

dependency_injector.providers.merge_dicts(dict1, dict2)

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(kwargs)

Parse named injections.

dependency_injector.providers.parse_positional_injections(args)

Parse positional injections.

dependency_injector.providers.represent_provider(provider, provides)

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.traverse(*providers, types=None)

Return providers traversal generator.