Skip to content

Commit d8439a2

Browse files
authored
Make provider generic type (ets-labs#293)
* Add __class_getitem__ for Provider to null the typing in runtime * Make Provider stub generic and remove types module * Update types module tests * Return types module with deprecation warning * Return types module with deprecation warning * Update changelog * Add docs page
1 parent f56b539 commit d8439a2

File tree

13 files changed

+4167
-3961
lines changed

13 files changed

+4167
-3961
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Key features of the ``Dependency Injector``:
7878
and dictionaries. See :ref:`configuration-provider`.
7979
- **Containers**. Provides declarative and dynamic containers. See :ref:`containers`.
8080
- **Performance**. Fast. Written in ``Cython``.
81-
- **Typing**. Provides typing stubs, ``mypy``-friendly.
81+
- **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`.
8282
- **Maturity**. Mature and production-ready. Well-tested, documented and supported.
8383

8484
.. code-block:: python

docs/introduction/key_features.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Key features of the ``Dependency Injector``:
2020
and dictionaries. See :ref:`configuration-provider`.
2121
- **Containers**. Provides declarative and dynamic containers. See :ref:`containers`.
2222
- **Performance**. Fast. Written in ``Cython``.
23-
- **Typing**. Provides typing stubs, ``mypy``-friendly.
23+
- **Typing**. Provides typing stubs, ``mypy``-friendly. See :ref:`provider-typing`.
2424
- **Maturity**. Mature and production-ready. Well-tested, documented and supported.
2525

2626
The framework stands on two principles:

docs/main/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
Develop
11+
-------
12+
- Add native support of the generics to the providers: ``some_provider = providers.Provider[SomeClass]``.
13+
- Deprecate module ``types``.
14+
- Add documentation page on providers typing and ``mypy`` support.
15+
1016
3.43.1
1117
------
1218
- Fix a typo in README.

docs/providers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ Providers module API docs - :py:mod:`dependency_injector.providers`
4949
overriding
5050
provided_instance
5151
custom
52+
typing_mypy

docs/providers/typing_mypy.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
.. _provider-typing:
2+
3+
Typing and mypy
4+
===============
5+
6+
.. meta::
7+
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Providers,Typing,Mypy,
8+
Pattern,Example
9+
:description: Dependency Injector providers are mypy-friendly. Providers module goes with the
10+
typing stubs to provide the typing information to ``mypy``, IDEs and editors.
11+
12+
Providers are ``mypy``-friendly.
13+
14+
Providers module goes with the typing stubs. It provides typing information to ``mypy`` and your
15+
IDE.
16+
17+
.. code-block:: python
18+
19+
from dependency_injector import providers
20+
21+
22+
class Animal:
23+
...
24+
25+
26+
class Cat(Animal)
27+
...
28+
29+
30+
provider = providers.Factory(Cat)
31+
32+
33+
if __name__ == '__main__':
34+
animal = provider() # mypy knows that animal is of type "Cat"
35+
36+
37+
You can use ``Provider`` as a generic type. This helps when a provider is an argument of a
38+
function or method.
39+
40+
.. code-block:: python
41+
:emphasize-lines: 12
42+
43+
from dependency_injector import providers
44+
45+
46+
class Animal:
47+
...
48+
49+
50+
class Cat(Animal)
51+
...
52+
53+
54+
provider: providers.Provider[Animal] = providers.Factory(Cat)
55+
56+
57+
if __name__ == '__main__':
58+
animal = provider() # mypy knows that animal is of type "Animal"

src/dependency_injector/containers.c

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)