dependency_injector.providers

Providers module.

class dependency_injector.providers.AbstractCallable

Bases: dependency_injector.providers.Callable

Abstract callable provider.

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

Overriding of AbstractCallable is possible only by another Callable provider.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.AbstractCoroutine

Bases: dependency_injector.providers.Coroutine

Abstract coroutine provider.

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

Overriding of AbstractCoroutine is possible only by another Coroutine provider.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.AbstractFactory

Bases: dependency_injector.providers.Factory

Abstract factory provider.

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

Overriding of AbstractFactory is possible only by another Factory provider.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.AbstractSingleton

Bases: dependency_injector.providers.BaseSingleton

Abstract singleton provider.

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

Overriding of AbstractSingleton is possible only by another BaseSingleton provider.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.AttributeGetter

Bases: dependency_injector.providers.Provider

Provider that returns the attribute of the injected instance.

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

async_()

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’s 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()

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

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

Set name of the attribute.

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.BaseSingleton

Bases: dependency_injector.providers.Provider

Base class of singleton providers.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Callable

Bases: dependency_injector.providers.Provider

Callable provider calls wrapped callable on every call.

Callable supports positional and keyword argument injections:

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

# or

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

# or

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

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.CallableDelegate

Bases: dependency_injector.providers.Delegate

Callable delegate injects delegating callable “as is”.

provides

Value that have to be provided.

Type

object

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Configuration

Bases: dependency_injector.providers.Object

Configuration provider provides configuration options to the other providers.

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

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’s delegate.

Return type

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

from_dict()

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

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.

Return type

None

from_ini()

Load configuration from the ini file.

Loaded configuration is merged recursively over existing configuration.

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

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

Return type

None

from_pydantic()

Load configuration from pydantic settings.

Loaded configuration is merged recursively over existing configuration.

Parameters
  • settings (pydantic.BaseSettings) – 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_yaml()

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.

Return type

None

get()

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

Return name.

get_strict()

Return strict flag.

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.

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

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

Override configuration option.

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

  • value (Any) – Overriding value

Returns

Overriding context.

Return type

OverridingContext

set_children()

Set children options.

set_default()

Set default.

set_name()

Set name.

set_provides()

Set provider’s provides.

set_strict()

Set strict flag.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

update()

Set configuration options.

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

Parameters

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

Return type

None

class dependency_injector.providers.ConfigurationOption

Bases: dependency_injector.providers.Provider

Child configuration option provider.

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

async_()

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’s delegate.

Return type

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

from_dict()

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

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.

Return type

None

from_ini()

Load configuration from the ini file.

Loaded configuration is merged recursively over existing configuration.

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

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

Return type

None

from_pydantic()

Load configuration from pydantic settings.

Loaded configuration is merged recursively over existing configuration.

Parameters
  • settings (pydantic.BaseSettings) – 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_yaml()

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.

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

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

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

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

update()

Set configuration options.

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

Parameters

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

Return type

None

class dependency_injector.providers.Container

Bases: dependency_injector.providers.Provider

Container provider provides an instance of declarative container.

Warning

Provider is experimental. Its interface may change.

apply_overridings()

Apply container overriding.

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

assign_parent()

Assign parent.

async_()

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’s 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.

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

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

Try to resolve provider name.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.ContextLocalSingleton

Bases: dependency_injector.providers.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()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Coroutine

Bases: dependency_injector.providers.Callable

Coroutine provider creates wrapped coroutine on every call.

Coroutine supports positional and keyword argument injections:

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

# or

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

# or

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

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.CoroutineDelegate

Bases: dependency_injector.providers.Delegate

Coroutine delegate injects delegating coroutine “as is”.

provides

Value that have to be provided.

Type

object

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Delegate

Bases: dependency_injector.providers.Provider

Delegate provider returns provider “as is”.

provides

Value that have to be provided.

Type

object

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedCallable

Bases: dependency_injector.providers.Callable

Callable that is injected “as is”.

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

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedCoroutine

Bases: dependency_injector.providers.Coroutine

Coroutine provider that is injected “as is”.

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

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedFactory

Bases: dependency_injector.providers.Factory

Factory that is injected “as is”.

provided_type

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

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedSingleton

Bases: dependency_injector.providers.Singleton

Delegated singleton is injected “as is”.

provided_type

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

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedThreadLocalSingleton

Bases: dependency_injector.providers.ThreadLocalSingleton

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

provided_type

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

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DelegatedThreadSafeSingleton

Bases: dependency_injector.providers.ThreadSafeSingleton

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

provided_type

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

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.DependenciesContainer

Bases: dependency_injector.providers.Object

DependenciesContainer provider provides set of dependencies.

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

Example code:

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

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

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

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

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

# Another file
from .containers import use_cases

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

Assign parent.

async_()

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’s 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.

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’s provides.

register_overrides()

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

Try to resolve provider name.

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Dependency

Bases: dependency_injector.providers.Provider

Dependency provider describes dependency interface.

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

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

database = database_provider()
instance_of

Class of required dependency.

type

type

assign_parent()

Assign parent.

async_()

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’s 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()

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

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

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

Set type.

set_instance_of()

Set type.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Dict

Bases: dependency_injector.providers.Provider

Dict provider provides a dictionary of values.

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

Positional argument injections are not supported.

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

dispatcher = dispatcher_factory()

# is equivalent to:

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

Add keyword argument injections.

Returns

Reference self

async_()

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’s 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()

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

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

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.EnvInterpolation

Bases: configparser.BasicInterpolation

Interpolation which expands environment variables in values.

class dependency_injector.providers.ExternalDependency

Bases: dependency_injector.providers.Dependency

ExternalDependency provider describes dependency interface.

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

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

database = database_provider()

Deprecated since version 3.9: Use Dependency instead.

instance_of

Class of required dependency.

type

type

assign_parent()

Assign parent.

async_()

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’s 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()

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

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

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

Set type.

set_instance_of()

Set type.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Factory

Bases: dependency_injector.providers.Provider

Factory provider creates new instance on every call.

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

Positional and keyword argument injections could be defined like this:

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

# or

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

# or

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

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

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

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

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

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

Type

type | None

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.FactoryAggregate

Bases: dependency_injector.providers.Provider

Factory providers aggregate.

FactoryAggregate is an aggregate of Factory providers.

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

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

async_()

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’s delegate.

Return type

Delegate

disable_async_mode()

Disable async mode.

enable_async_mode()

Enable async mode.

factories

Return dictionary of factories, read-only.

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

register_overrides()

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

Set factories.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.FactoryDelegate

Bases: dependency_injector.providers.Delegate

Factory delegate injects delegating factory “as is”.

provides

Value that have to be provided.

Type

object

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Injection

Bases: object

Abstract injection class.

class dependency_injector.providers.ItemGetter

Bases: dependency_injector.providers.Provider

Provider that returns the item of the injected instance.

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

async_()

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’s 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()

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

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

Set name of the item.

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.List

Bases: dependency_injector.providers.Provider

List provider provides a list of values.

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

Keyword argument injections are not supported.

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

dispatcher = dispatcher_factory()

# is equivalent to:

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

Add positional argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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.

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.MethodCaller

Bases: dependency_injector.providers.Provider

Provider that calls the method of the injected instance.

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

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.NamedInjection

Bases: dependency_injector.providers.Injection

Keyword injection class.

get_name()

Return injection name.

get_original_value()

Return original value.

get_value()

Return injection value.

set()

Set injection.

set_name()

Set injection name.

class dependency_injector.providers.Object

Bases: dependency_injector.providers.Provider

Object provider returns provided instance “as is”.

provides

Value that have to be provided.

Type

object

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

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: dependency_injector.providers.Injection

Positional injection class.

get_original_value()

Return original value.

get_value()

Return injection value.

set()

Set injection.

class dependency_injector.providers.ProvidedInstance

Bases: dependency_injector.providers.Provider

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

You can use it like that:

service = providers.Singleton(Service)

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

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

Providers that have .provided attribute:

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

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’s subclasses.

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

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

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

provider1.override(provider2)

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

Also Provider implements helper function for creating its delegates:

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

delegated = delegate()

assert provider is delegated

All providers should extend this class.

overridden

Tuple of overriding providers, if any.

type

tuple[Provider] | None

async_()

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’s 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.

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

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

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Resource

Bases: dependency_injector.providers.Provider

Resource provider provides a component with initialization and shutdown.

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

shutdown()

Shutdown resource.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Selector

Bases: dependency_injector.providers.Provider

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

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

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

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

config = Configuration()

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

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

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

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’s 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.

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

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

Set providers.

set_selector()

Set selector.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Self

Bases: dependency_injector.providers.Provider

Self provider returns own container.

async_()

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’s 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.

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

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

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.Singleton

Bases: dependency_injector.providers.BaseSingleton

Singleton provider returns same instance on every call.

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

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

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

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

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.SingletonDelegate

Bases: dependency_injector.providers.Delegate

Singleton delegate injects delegating singleton “as is”.

provides

Value that have to be provided.

Type

object

async_()

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’s 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.

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

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

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.ThreadLocalSingleton

Bases: dependency_injector.providers.BaseSingleton

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

provided_type

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

Type

type | None

cls

Class that provides object. Alias for provides.

type

type

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.ThreadSafeSingleton

Bases: dependency_injector.providers.BaseSingleton

Thread-safe singleton provider.

add_args()

Add __init__ positional argument injections.

Returns

Reference self

add_attributes()

Add attribute injections.

Returns

Reference self

add_kwargs()

Add __init__ keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set __init__ positional argument injections.

Existing __init__ positional argument injections are dropped.

Returns

Reference self

set_attributes()

Set attribute injections.

Existing attribute injections are dropped.

Returns

Reference self

set_kwargs()

Set __init__ keyword argument injections.

Existing __init__ keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.TypedConfigurationOption

Bases: dependency_injector.providers.Callable

add_args()

Add positional argument injections.

Returns

Reference self

add_kwargs()

Add keyword argument injections.

Returns

Reference self

args

Return positional argument injections.

async_()

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’s 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()

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

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

Set positional argument injections.

Existing positional argument injections are dropped.

Returns

Reference self

set_kwargs()

Set keyword argument injections.

Existing keyword argument injections are dropped.

Returns

Reference self

set_provides()

Set provider’s provides.

traverse()

Return providers traversal generator.

unregister_overrides()

Unregister provider that overrides current provider.

class dependency_injector.providers.YamlLoader(stream)

Bases: yaml.loader.SafeLoader

Custom YAML loader.

Inherits yaml.SafeLoader and add environment variables interpolation.

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

Return full copy of provider or container with providers.

dependency_injector.providers.ensure_is_provider()

Check if instance is provider instance and return it.

Parameters

instance (object) – Instance to be checked.

Raise

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

Return type

dependency_injector.providers.Provider

dependency_injector.providers.is_container_class()

Check if instance is container class.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.providers.is_container_instance()

Check if instance is container instance.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.providers.is_delegated()

Check if instance is delegated provider.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.providers.is_provider()

Check if instance is provider instance.

Parameters

instance (object) – Instance to be checked.

Return type

bool

dependency_injector.providers.isasyncgenfunction()

Check if object is an asynchronous generator function.

Return False for any object in Python 3.4 or below.

dependency_injector.providers.isawaitable()

Check if object is a coroutine function.

Return False for any object in Python 3.4 or below.

dependency_injector.providers.iscoroutinefunction()

Check if object is a coroutine function.

Return False for any object in Python 3.4 or below.

dependency_injector.providers.merge_dicts()

Merge dictionaries recursively.

Parameters
  • dict1 (dict) – Dictionary 1

  • dict2 (dict) – Dictionary 2

Returns

New resulting dictionary

Return type

dict

dependency_injector.providers.parse_named_injections()

Parse named injections.

dependency_injector.providers.parse_positional_injections()

Parse positional injections.

dependency_injector.providers.represent_provider()

Return string representation of provider.

Parameters
  • provider (object) – Provider object

  • provides – Object that provider provides

Returns

String representation of provider

Return type

str

dependency_injector.providers.traverse()

Return providers traversal generator.

dependency_injector.providers.yaml_env_marker_constructor()

“Replace environment variable marker with its value.