Providers help to assemble the objects. They create objects and inject the dependencies.
Each provider is a callable. You call the provider like a function when you need to create an
object. Provider retrieves the underlying dependencies and inject them into the created object.
It causes the cascade effect that helps to assemble object graphs. See
Callable and other provider docs below.
provider1() │ ├──> provider2() │ ├──> provider3() │ │ │ └──> provider4() │ └──> provider5() │ └──> provider6()
Another providers feature is an overriding. You can override any provider with another provider. This helps in testing. This also helps in overriding API clients with stubs for the development or staging environment. See the example at Provider overriding.
If you need to inject not the whole object but the parts see Injecting provided object attributes, items, or call its methods.
To create a new provider see Creating a custom provider.
Providers module API docs -
- Factory provider
- Singleton provider
- Callable provider
- Coroutine provider
- Object provider
- List provider
- Dict provider
- Configuration provider
- Loading from an INI file
- Loading from a YAML file
- Loading from a Pydantic settings
- Loading from a dictionary
- Loading from an environment variable
- Loading a value
- Loading from the multiple sources
- Using environment variables in configuration files
- Mandatory and optional sources
- Specifying the value type
- Strict mode and required options
- Injecting invariants
- Resource provider
- Aggregate provider
- Selector provider
- Dependency provider
- Provider overriding
- Injecting provided object attributes, items, or call its methods
- Injecting container “self”
- Creating a custom provider
- Asynchronous injections
- Typing and mypy