Add typing module and object provider stubs

This commit is contained in:
Roman Mogylatov 2020-08-25 15:48:29 -04:00
parent fd7d39de5b
commit 3288fde6b0
5 changed files with 147 additions and 115 deletions

View File

@ -1,3 +1,7 @@
[flake8] [flake8]
max_line_length = 99 max_line_length = 100
max_complexity = 10 max_complexity = 10
exclude = types.py
[pydocstyle]
ignore = D100,D101,D102,D105,D106,D107,D203,D213

View File

@ -1,112 +1 @@
from __future__ import annotations from .types import *
from typing import TypeVar, Generic, Type, Callable as _Callable, Any, Tuple, Optional, Dict, Union
Injection = Any
T = TypeVar('T')
class OverridingContext:
def __init__(self, overridden: Provider, overriding: Provider): ...
def __enter__(self) -> Provider: ...
def __exit__(self, *_: Any) -> None: ...
class Provider:
def __init__(self) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
@property
def overridden(self) -> Tuple[Provider]: ...
@property
def last_overriding(self) -> Optional[Provider]: ...
def override(self, provider: Union[Provider, Any]) -> OverridingContext: ...
def reset_last_overriding(self) -> None: ...
def reset_override(self) -> None: ...
def delegate(self) -> Provider: ...
@property
def provider(self) -> Provider: ...
class Callable(Provider, Generic[T]):
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@property
def provides(self) -> T: ...
@property
def provided(self) -> ProvidedInstance: ...
@property
def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> Callable[T]: ...
def set_args(self, *args: Injection) -> Callable[T]: ...
def clear_args(self) -> Callable[T]: ...
@property
def kwargs(self) -> Dict[str, Injection]: ...
def add_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
def set_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
def clear_kwargs(self) -> Callable[T]: ...
class DelegatedCallable(Callable): ...
class AbstractCallable(Callable):
def override(self, provider: Callable) -> OverridingContext: ...
class Factory(Provider, Generic[T]):
provided_type: Optional[Type]
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@property
def cls(self) -> T: ...
@property
def provides(self) -> T: ...
@property
def provided(self) -> ProvidedInstance: ...
@property
def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> Factory[T]: ...
def set_args(self, *args: Injection) -> Factory[T]: ...
def clear_args(self) -> Factory[T]: ...
@property
def kwargs(self) -> Dict[str, Injection]: ...
def add_kwargs(self, **kwargs: Injection) -> Factory[T]: ...
def set_kwargs(self, **kwargs: Injection) -> Factory[T]: ...
def clear_kwargs(self) -> Factory[T]: ...
@property
def attributes(self) -> Dict[str, Injection]: ...
def add_attributes(self, **kwargs: Injection) -> Factory[T]: ...
def set_attributes(self, **kwargs: Injection) -> Factory[T]: ...
def clear_attributes(self) -> Factory[T]: ...
class DelegatedFactory(Factory): ...
class AbstractFactory(Factory):
def override(self, provider: Factory) -> OverridingContext: ...
class ProvidedInstanceFluentInterface:
def __getattr__(self, item: str) -> AttributeGetter: ...
def __getitem__(self, item: str) -> ItemGetter: ...
def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ...
class ProvidedInstance(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider) -> None: ...
class AttributeGetter(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider, attribute: str) -> None: ...
class ItemGetter(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider, item: str) -> None: ...
class MethodCaller(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider, *args: Injection, **kwargs: Injection) -> None: ...

View File

@ -0,0 +1,119 @@
from __future__ import annotations
from typing import TypeVar, Generic, Type, Callable as _Callable, Any, Tuple, Optional, Dict, Union
Injection = Any
T = TypeVar('T')
class OverridingContext:
def __init__(self, overridden: Provider, overriding: Provider): ...
def __enter__(self) -> Provider: ...
def __exit__(self, *_: Any) -> None: ...
class Provider:
def __init__(self) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ...
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
@property
def overridden(self) -> Tuple[Provider]: ...
@property
def last_overriding(self) -> Optional[Provider]: ...
def override(self, provider: Union[Provider, Any]) -> OverridingContext: ...
def reset_last_overriding(self) -> None: ...
def reset_override(self) -> None: ...
def delegate(self) -> Provider: ...
@property
def provider(self) -> Provider: ...
class Object(Provider, Generic[T]):
def __init__(self, provides: T) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@property
def provided(self) -> ProvidedInstance: ...
class Callable(Provider, Generic[T]):
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@property
def provides(self) -> T: ...
@property
def provided(self) -> ProvidedInstance: ...
@property
def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> Callable[T]: ...
def set_args(self, *args: Injection) -> Callable[T]: ...
def clear_args(self) -> Callable[T]: ...
@property
def kwargs(self) -> Dict[str, Injection]: ...
def add_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
def set_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
def clear_kwargs(self) -> Callable[T]: ...
class DelegatedCallable(Callable): ...
class AbstractCallable(Callable):
def override(self, provider: Callable) -> OverridingContext: ...
class Factory(Provider, Generic[T]):
provided_type: Optional[Type]
def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ...
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@property
def cls(self) -> T: ...
@property
def provides(self) -> T: ...
@property
def provided(self) -> ProvidedInstance: ...
@property
def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> Factory[T]: ...
def set_args(self, *args: Injection) -> Factory[T]: ...
def clear_args(self) -> Factory[T]: ...
@property
def kwargs(self) -> Dict[str, Injection]: ...
def add_kwargs(self, **kwargs: Injection) -> Factory[T]: ...
def set_kwargs(self, **kwargs: Injection) -> Factory[T]: ...
def clear_kwargs(self) -> Factory[T]: ...
@property
def attributes(self) -> Dict[str, Injection]: ...
def add_attributes(self, **kwargs: Injection) -> Factory[T]: ...
def set_attributes(self, **kwargs: Injection) -> Factory[T]: ...
def clear_attributes(self) -> Factory[T]: ...
class DelegatedFactory(Factory): ...
class AbstractFactory(Factory):
def override(self, provider: Factory) -> OverridingContext: ...
class ProvidedInstanceFluentInterface:
def __getattr__(self, item: str) -> AttributeGetter: ...
def __getitem__(self, item: str) -> ItemGetter: ...
def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ...
class ProvidedInstance(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider) -> None: ...
class AttributeGetter(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider, attribute: str) -> None: ...
class ItemGetter(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider, item: str) -> None: ...
class MethodCaller(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provider: Provider, *args: Injection, **kwargs: Injection) -> None: ...

View File

@ -1,14 +1,17 @@
from typing import Tuple, Any, Dict from typing import Tuple, Any, Dict
from dependency_injector import providers from dependency_injector import providers, types
class Animal: class Animal:
xyz: int = 123
... ...
class Cat(Animal): class Cat(Animal):
def __init__(self, *_, **__): ...
@classmethod @classmethod
def create(cls) -> Animal: def create(cls) -> Animal:
return cls() return cls()
@ -38,7 +41,7 @@ provider5 = providers.Factory(Animal)
provided5: providers.ProvidedInstance = provider5.provided provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided['item'] item_getter5: providers.ItemGetter = provider5.provided['item']
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324) method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
# Test 6: to check the DelegatedFactory # Test 6: to check the DelegatedFactory
provider6 = providers.DelegatedFactory(Cat) provider6 = providers.DelegatedFactory(Cat)
@ -48,3 +51,7 @@ animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0)
provider7 = providers.AbstractFactory(Animal) provider7 = providers.AbstractFactory(Animal)
provider7.override(providers.Factory(Cat)) provider7.override(providers.Factory(Cat))
animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0) animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0)
# Test 8: to check the explicit typing
provider8: types.Factory[Animal] = lambda: None
animal8: int = provider8()

13
tests/typing/object.py Normal file
View File

@ -0,0 +1,13 @@
from dependency_injector import providers
# Test 1: to check the return type
provider1 = providers.Object(int(3))
var1: int = provider1()
# Test 2: to check the provided instance interface
provider2 = providers.Object(int)
provided2: providers.ProvidedInstance = provider2.provided
attr_getter2: providers.AttributeGetter = provider2.provided.attr
item_getter2: providers.ItemGetter = provider2.provided['item']
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)