2020-07-11 19:15:00 +03:00
|
|
|
=================================================================
|
|
|
|
Dependency Injector --- Dependency injection framework for Python
|
|
|
|
=================================================================
|
2015-04-02 16:17:00 +03:00
|
|
|
|
2016-02-02 00:13:55 +03:00
|
|
|
.. meta::
|
2020-07-29 19:18:09 +03:00
|
|
|
:google-site-verification: V1hlKfpgL3AARAElwFcqP4qW1Smsx5bKSRU8O86i20Y
|
2017-02-01 14:07:44 +03:00
|
|
|
:keywords: Python,Dependency injection,DI,Inversion of Control,IoC,
|
|
|
|
IoC Container,Factory, Singleton, Design Patterns
|
2020-07-29 19:18:09 +03:00
|
|
|
:description: Dependency Injector is a dependency injection framework
|
2020-08-08 21:48:05 +03:00
|
|
|
for Python. It helps to maintain you application structure.
|
|
|
|
It was designed to be unified, developer-friendly
|
2017-02-01 14:07:44 +03:00
|
|
|
tool that helps to implement dependency injection design
|
|
|
|
pattern in formal, pretty, Pythonic way. Dependency Injector
|
|
|
|
provides implementations of such popular design patterns
|
|
|
|
like IoC container, Factory and Singleton. Dependency
|
|
|
|
Injector providers are implemented as C extension types
|
|
|
|
using Cython.
|
2016-11-11 18:05:25 +03:00
|
|
|
|
|
|
|
.. _index:
|
2016-02-02 00:13:55 +03:00
|
|
|
|
2020-07-03 05:07:07 +03:00
|
|
|
.. image:: https://img.shields.io/pypi/v/dependency_injector.svg
|
|
|
|
:target: https://pypi.org/project/dependency-injector/
|
|
|
|
:alt: Latest Version
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-03 05:07:07 +03:00
|
|
|
.. image:: https://img.shields.io/pypi/l/dependency_injector.svg
|
|
|
|
:target: https://pypi.org/project/dependency-injector/
|
|
|
|
:alt: License
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-03 05:07:07 +03:00
|
|
|
.. image:: https://img.shields.io/pypi/pyversions/dependency_injector.svg
|
|
|
|
:target: https://pypi.org/project/dependency-injector/
|
|
|
|
:alt: Supported Python versions
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-03 05:07:07 +03:00
|
|
|
.. image:: https://img.shields.io/pypi/implementation/dependency_injector.svg
|
|
|
|
:target: https://pypi.org/project/dependency-injector/
|
|
|
|
:alt: Supported Python implementations
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-10-21 23:54:10 +03:00
|
|
|
.. image:: https://static.pepy.tech/badge/dependency-injector
|
2020-07-11 19:15:00 +03:00
|
|
|
:target: https://pepy.tech/project/dependency-injector
|
|
|
|
:alt: Downloads
|
|
|
|
|
2020-10-21 23:54:10 +03:00
|
|
|
.. image:: https://static.pepy.tech/badge/dependency-injector/month
|
2020-07-11 19:15:00 +03:00
|
|
|
:target: https://pepy.tech/project/dependency-injector
|
|
|
|
:alt: Downloads
|
|
|
|
|
2020-10-21 23:54:10 +03:00
|
|
|
.. image:: https://static.pepy.tech/badge/dependency-injector/week
|
2020-07-11 19:15:00 +03:00
|
|
|
:target: https://pepy.tech/project/dependency-injector
|
|
|
|
:alt: Downloads
|
|
|
|
|
|
|
|
.. image:: https://img.shields.io/pypi/wheel/dependency-injector.svg
|
|
|
|
:target: https://pypi.org/project/dependency-injector/
|
|
|
|
:alt: Wheel
|
|
|
|
|
2021-01-27 15:38:35 +03:00
|
|
|
.. image:: https://img.shields.io/github/workflow/status/ets-labs/python-dependency-injector/Tests%20and%20linters/master
|
|
|
|
:target: https://github.com/ets-labs/python-dependency-injector/actions
|
2020-07-03 05:07:07 +03:00
|
|
|
:alt: Build Status
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-03 05:07:07 +03:00
|
|
|
.. image:: https://coveralls.io/repos/github/ets-labs/python-dependency-injector/badge.svg?branch=master
|
|
|
|
:target: https://coveralls.io/github/ets-labs/python-dependency-injector?branch=master
|
|
|
|
:alt: Coverage Status
|
|
|
|
|
2020-07-18 07:40:14 +03:00
|
|
|
``Dependency Injector`` is a dependency injection framework for Python.
|
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
It helps implementing the dependency injection principle.
|
2020-08-11 05:29:25 +03:00
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
Key features of the ``Dependency Injector``:
|
2020-08-11 05:29:25 +03:00
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
- **Providers**. Provides ``Factory``, ``Singleton``, ``Callable``, ``Coroutine``, ``Object``,
|
2020-10-25 03:56:32 +03:00
|
|
|
``List``, ``Dict``, ``Configuration``, ``Resource``, ``Dependency`` and ``Selector`` providers
|
|
|
|
that help assembling your objects. See :ref:`providers`.
|
2020-09-03 23:19:40 +03:00
|
|
|
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
|
|
|
|
and configuring dev / stage environment to replace API clients with stubs etc. See
|
|
|
|
:ref:`provider-overriding`.
|
2021-02-03 17:21:32 +03:00
|
|
|
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
|
|
|
|
environment variables, and dictionaries. See :ref:`configuration-provider`.
|
2020-10-25 03:56:32 +03:00
|
|
|
- **Resources**. Helps with initialization and configuring of logging, event loop, thread
|
2020-10-30 05:55:09 +03:00
|
|
|
or process pool, etc. Can be used for per-function execution scope in tandem with wiring.
|
|
|
|
See :ref:`resource-provider`.
|
2020-09-03 23:19:40 +03:00
|
|
|
- **Containers**. Provides declarative and dynamic containers. See :ref:`containers`.
|
2020-10-09 22:16:27 +03:00
|
|
|
- **Wiring**. Injects dependencies into functions and methods. Helps integrating with
|
2020-11-16 00:06:42 +03:00
|
|
|
other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc. See :ref:`wiring`.
|
2021-01-11 03:26:15 +03:00
|
|
|
- **Asynchronous**. Supports asynchronous injections. See :ref:`async-injections`.
|
2020-09-14 03:32:21 +03:00
|
|
|
- **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`.
|
2020-10-09 22:16:27 +03:00
|
|
|
- **Performance**. Fast. Written in ``Cython``.
|
2020-09-04 01:03:21 +03:00
|
|
|
- **Maturity**. Mature and production-ready. Well-tested, documented and supported.
|
2020-08-11 05:29:25 +03:00
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
.. code-block:: python
|
2020-08-11 05:29:25 +03:00
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-11-16 00:06:42 +03:00
|
|
|
from dependency_injector.wiring import inject, Provide
|
2020-08-11 05:29:25 +03:00
|
|
|
|
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
config = providers.Configuration()
|
|
|
|
|
|
|
|
api_client = providers.Singleton(
|
|
|
|
ApiClient,
|
|
|
|
api_key=config.api_key,
|
|
|
|
timeout=config.timeout.as_int(),
|
|
|
|
)
|
|
|
|
|
|
|
|
service = providers.Factory(
|
|
|
|
Service,
|
|
|
|
api_client=api_client,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-16 00:06:42 +03:00
|
|
|
@inject
|
2020-10-09 22:16:27 +03:00
|
|
|
def main(service: Service = Provide[Container.service]):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-09-03 23:19:40 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
container = Container()
|
|
|
|
container.config.api_key.from_env('API_KEY')
|
|
|
|
container.config.timeout.from_env('TIMEOUT')
|
2020-10-09 22:16:27 +03:00
|
|
|
container.wire(modules=[sys.modules[__name__]])
|
2020-09-03 23:19:40 +03:00
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
main() # <-- dependency is injected automatically
|
2020-07-03 05:07:07 +03:00
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
with container.api_client.override(mock.Mock()):
|
|
|
|
main() # <-- overridden dependency is injected automatically
|
2016-02-04 23:47:45 +03:00
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
With the ``Dependency Injector`` objects assembling is consolidated in the container.
|
|
|
|
Dependency injections are defined explicitly.
|
|
|
|
This makes easier to understand and change how application works.
|
2016-02-04 23:47:45 +03:00
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
.. figure:: https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/di-readme.svg
|
|
|
|
:target: https://github.com/ets-labs/python-dependency-injector
|
2016-02-04 23:47:45 +03:00
|
|
|
|
2020-07-18 07:40:14 +03:00
|
|
|
Explore the documentation to know more about the ``Dependency Injector``.
|
2016-11-11 18:05:25 +03:00
|
|
|
|
2020-07-20 23:58:18 +03:00
|
|
|
.. _contents:
|
|
|
|
|
2015-04-02 16:17:00 +03:00
|
|
|
Contents
|
|
|
|
--------
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 2
|
|
|
|
|
|
|
|
introduction/index
|
|
|
|
examples/index
|
|
|
|
tutorials/index
|
|
|
|
providers/index
|
|
|
|
containers/index
|
|
|
|
wiring
|
|
|
|
examples-other/index
|
|
|
|
api/index
|
|
|
|
main/feedback
|
|
|
|
main/changelog
|