Skip to content

Commit 2e940ad

Browse files
authored
Typing stubs (ets-labs#286)
* Add basic setup * Add more tests for factory * Add mypy checks to CI * Add mypy checks to makefile command * Add typing for the factories * Add stub for Callable providers * Add typing module and object provider stubs * Fix typing test issue * Remove typing module * Add Delegate stub * Add stub for Dependency provider * Add stub for ExternalDependency * Add stubs for providers module functions * Add stubs for the DependenciesContainer provider * Add stub for the CallableDelegate provider * Add stubs for Coroutine providers * Add stubs for the configuration options * Add stub for the FactoryDelegate * Add stub for the FactoryAggregate provider * Add singleton stubs * Add stubs for singletons * Add stub for the List provider * Add stub for the Container provider * Add stub for the Selector provider * Add stubs for the dynamic container * Add stub for the declarative container * Add stubs for the extensions * Add types module for explicit provider typing * Set absolute import mode for the providers module and add types module test * Skip typing test for Python 3.5 * Remove coroutine test from py35 * Fix py35 tests * Add \n to the tox.ini
1 parent 47f4279 commit 2e940ad

32 files changed

+4831
-4005
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ jobs:
2222
env: TOXENV=pydocstyle
2323
install: pip install tox
2424
script: tox
25+
- python: 3.6
26+
env: TOXENV=mypy
27+
install: pip install tox
28+
script: tox
2529
- python: 2.7
2630
env: TOXENV=py27
2731
install: pip install tox

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
recursive-include src/dependency_injector *.py *.pyx *.pxd *.c
1+
recursive-include src/dependency_injector *.py* *.c
22
recursive-include tests *.py
33
include README.rst
44
include CONTRIBUTORS.rst
55
include LICENSE.rst
66
include requirements.txt
77
include setup.py
88
include tox.ini
9+
include py.typed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ test-py3: build
6060
coverage html --rcfile=./.coveragerc
6161

6262
check:
63-
# Static analysis
6463
flake8 src/dependency_injector/
6564
flake8 examples/
66-
# Code style analysis
65+
6766
pydocstyle src/dependency_injector/
6867
pydocstyle examples/
6968

69+
mypy tests/typing
70+
7071
test-publish: cythonize
7172
# Create distributions
7273
python setup.py sdist

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ flake8
66
pydocstyle
77
sphinx_autobuild
88
pip
9+
mypy
910

1011
-r requirements-ext.txt

setup.cfg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[flake8]
2-
max_line_length = 99
2+
max_line_length = 100
33
max_complexity = 10
4+
exclude = types.py
5+
6+
[pydocstyle]
7+
ignore = D100,D101,D102,D105,D106,D107,D203,D213

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
'': 'src',
4848
},
4949
package_data={
50-
'dependency_injector': ['*.pxd'],
50+
'dependency_injector': ['*.pxd', '*.pyi', 'py.typed'],
5151
},
5252
ext_modules=[
5353
Extension('dependency_injector.containers',

src/dependency_injector/__init__.pyi

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import Type, Dict, Tuple, Optional, Any, Union, ClassVar, Callable as _Callable
2+
3+
from .providers import Provider
4+
5+
6+
class Container:
7+
provider_type: Type[Provider] = Provider
8+
providers: Dict[str, Provider]
9+
overridden: Tuple[Provider]
10+
def __init__(self) -> None: ...
11+
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
12+
def __setattr__(self, name: str, value: Union[Provider, Any]) -> None: ...
13+
def __delattr__(self, name: str) -> None: ...
14+
def set_providers(self, **providers: Provider): ...
15+
def override(self, overriding: DynamicContainer) -> None: ...
16+
def override_providers(self, **overriding_providers: Provider) -> None: ...
17+
def reset_last_overriding(self) -> None: ...
18+
def reset_override(self) -> None: ...
19+
20+
21+
class DynamicContainer(Container): ...
22+
23+
24+
class DeclarativeContainer(Container):
25+
cls_providers: ClassVar[Dict[str, Provider]]
26+
inherited_providers: ClassVar[Dict[str, Provider]]
27+
28+
29+
def override(container: Container) -> _Callable[[Container], Container]: ...
30+
31+
32+
def copy(container: Container) -> _Callable[[Container], Container]: ...
33+
34+
def is_container(instance: Any) -> bool: ...

src/dependency_injector/ext/__init__.pyi

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import Awaitable as _Awaitable
2+
3+
from dependency_injector import providers
4+
5+
6+
class Application(providers.Singleton): ...
7+
8+
9+
class Extension(providers.Singleton): ...
10+
11+
12+
class Middleware(providers.DelegatedCallable): ...
13+
14+
15+
class MiddlewareFactory(providers.Factory): ...
16+
17+
18+
class View(providers.Callable):
19+
def as_view(self) -> _Awaitable: ...
20+
21+
22+
class ClassBasedView(providers.Factory):
23+
def as_view(self) -> _Awaitable: ...

0 commit comments

Comments
 (0)