Add docs page

This commit is contained in:
Roman Mogylatov 2020-09-13 20:31:30 -04:00
parent a52729177f
commit 878ccf36c9
5 changed files with 62 additions and 2 deletions

View File

@ -78,7 +78,7 @@ Key features of the ``Dependency Injector``:
and dictionaries. See :ref:`configuration-provider`. and dictionaries. See :ref:`configuration-provider`.
- **Containers**. Provides declarative and dynamic containers. See :ref:`containers`. - **Containers**. Provides declarative and dynamic containers. See :ref:`containers`.
- **Performance**. Fast. Written in ``Cython``. - **Performance**. Fast. Written in ``Cython``.
- **Typing**. Provides typing stubs, ``mypy``-friendly. - **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`.
- **Maturity**. Mature and production-ready. Well-tested, documented and supported. - **Maturity**. Mature and production-ready. Well-tested, documented and supported.
.. code-block:: python .. code-block:: python

View File

@ -20,7 +20,7 @@ Key features of the ``Dependency Injector``:
and dictionaries. See :ref:`configuration-provider`. and dictionaries. See :ref:`configuration-provider`.
- **Containers**. Provides declarative and dynamic containers. See :ref:`containers`. - **Containers**. Provides declarative and dynamic containers. See :ref:`containers`.
- **Performance**. Fast. Written in ``Cython``. - **Performance**. Fast. Written in ``Cython``.
- **Typing**. Provides typing stubs, ``mypy``-friendly. - **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`.
- **Maturity**. Mature and production-ready. Well-tested, documented and supported. - **Maturity**. Mature and production-ready. Well-tested, documented and supported.
The framework stands on two principles: The framework stands on two principles:

View File

@ -11,6 +11,7 @@ Develop
------- -------
- Add native support of the generics to the providers: ``some_provider = providers.Provider[SomeClass]``. - Add native support of the generics to the providers: ``some_provider = providers.Provider[SomeClass]``.
- Deprecate module ``types``. - Deprecate module ``types``.
- Add documentation page on providers typing and ``mypy`` support.
3.43.1 3.43.1
------ ------

View File

@ -49,3 +49,4 @@ Providers module API docs - :py:mod:`dependency_injector.providers`
overriding overriding
provided_instance provided_instance
custom custom
typing_mypy

View File

@ -0,0 +1,58 @@
.. _provider-typing:
Typing and mypy
===============
.. meta::
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Providers,Typing,Mypy,
Pattern,Example
:description: Dependency Injector providers are mypy-friendly. Providers module goes with the
typing stubs to provide the typing information to ``mypy``, IDEs and editors.
Providers are ``mypy``-friendly.
Providers module goes with the typing stubs. It provides typing information to ``mypy`` and your
IDE.
.. code-block:: python
from dependency_injector import providers
class Animal:
...
class Cat(Animal)
...
provider = providers.Factory(Cat)
if __name__ == '__main__':
animal = provider() # mypy knows that animal is of type "Cat"
You can use ``Provider`` as a generic type. This helps when a provider is an argument of a
function or method.
.. code-block:: python
:emphasize-lines: 12
from dependency_injector import providers
class Animal:
...
class Cat(Animal)
...
provider: providers.Provider[Animal] = providers.Factory(Cat)
if __name__ == '__main__':
animal = provider() # mypy knows that animal is of type "Animal"