Make mypy --strict tests/typing passable

This commit is contained in:
ZipFile 2025-05-30 19:31:44 +00:00
parent 1271d0fa79
commit 7fcf1ac7ad
19 changed files with 127 additions and 83 deletions

View File

@ -1,12 +1,10 @@
from dependency_injector import providers
class Animal:
...
class Animal: ...
class Cat(Animal):
...
class Cat(Animal): ...
# Test 1: to check Aggregate provider
@ -20,13 +18,19 @@ val1: str = provider1("a")
provider1_set_non_string_keys: providers.Aggregate[str] = providers.Aggregate()
provider1_set_non_string_keys.set_providers({Cat: providers.Object("str")})
provider_set_non_string_1: providers.Provider[str] = provider1_set_non_string_keys.providers[Cat]
provider_set_non_string_1: providers.Provider[str] = (
provider1_set_non_string_keys.providers[Cat]
)
provider1_new_non_string_keys: providers.Aggregate[str] = providers.Aggregate(
{Cat: providers.Object("str")},
)
factory_new_non_string_1: providers.Provider[str] = provider1_new_non_string_keys.providers[Cat]
factory_new_non_string_1: providers.Provider[str] = (
provider1_new_non_string_keys.providers[Cat]
)
provider1_no_explicit_typing = providers.Aggregate(a=providers.Object("str"))
provider1_no_explicit_typing_factory: providers.Provider[str] = provider1_no_explicit_typing.providers["a"]
provider1_no_explicit_typing_factory: providers.Provider[str] = (
provider1_no_explicit_typing.providers["a"]
)
provider1_no_explicit_typing_object: str = provider1_no_explicit_typing("a")

View File

@ -1,10 +1,9 @@
from typing import Callable, Optional, Tuple, Any, Dict, Type
from typing import Any, Callable, Dict, Optional, Tuple, Type
from dependency_injector import providers
class Animal:
...
class Animal: ...
class Cat(Animal):
@ -53,10 +52,13 @@ provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
# Test 9: to check the return type with await
provider9 = providers.Callable(Cat)
async def _async9() -> None:
animal1: Animal = await provider9(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
animal2: Animal = await provider9.async_(1, 2, 3, b="1", c=2, e=0.0)
# Test 10: to check the .provides
provider10 = providers.Callable(Cat)
provides10: Optional[Callable[..., Cat]] = provider10.provides
@ -68,5 +70,5 @@ provides11: Optional[Callable[..., Animal]] = provider11.provides
assert provides11 is Cat
# Test 12: to check string imports
provider12: providers.Callable[dict] = providers.Callable("builtins.dict")
provider12: providers.Callable[Dict[Any, Any]] = providers.Callable("builtins.dict")
provider12.set_provides("builtins.dict")

View File

@ -1,13 +1,13 @@
from pathlib import Path
from typing import Any
from typing import Any, Dict
from dependency_injector import providers
from pydantic_settings import BaseSettings as PydanticSettings
from dependency_injector import providers
# Test 1: to check the getattr
config1: providers.Configuration = providers.Configuration()
provider1: providers.Provider[dict] = providers.Factory(dict, a=config1.a)
provider1: providers.Provider[Dict[str, Any]] = providers.Factory(dict, a=config1.a)
# Test 2: to check the from_*() method
config2 = providers.Configuration()
@ -68,7 +68,9 @@ config5_pydantic = providers.Configuration(
pydantic_settings=[PydanticSettings()],
)
config5_pydantic.set_pydantic_settings([PydanticSettings()])
config5_pydantic_settings: list[PydanticSettings] = config5_pydantic.get_pydantic_settings()
config5_pydantic_settings: list[PydanticSettings] = (
config5_pydantic.get_pydantic_settings()
)
# Test 6: to check init arguments
config6 = providers.Configuration(

View File

@ -1,8 +1,9 @@
from typing import Any
from dependency_injector import providers
class Container:
...
class Container: ...
# Test 1: to check the return type
@ -11,4 +12,4 @@ var1: Container = provider1()
# Test 2: to check the getattr
provider2 = providers.Container(Container)
attr: providers.Provider = provider2.attr
attr: providers.Provider[Any] = provider2.attr

View File

@ -1,14 +1,14 @@
from typing import Coroutine
from typing import Awaitable, Coroutine
from dependency_injector import providers
async def _coro() -> None:
...
async def _coro() -> None: ...
# Test 1: to check the return type
provider1 = providers.Coroutine(_coro)
var1: Coroutine = provider1()
var1: Awaitable[None] = provider1()
# Test 2: to check string imports
provider2: providers.Coroutine[None] = providers.Coroutine("_coro")

View File

@ -1,4 +1,4 @@
from typing import Dict
from typing import Any, Dict
from dependency_injector import containers, providers
@ -10,7 +10,7 @@ class Container1(containers.DeclarativeContainer):
container1 = Container1()
container1_type: containers.Container = Container1()
provider1: providers.Provider = container1.provider
provider1: providers.Provider[int] = container1.provider
val1: int = container1.provider(3)
@ -20,8 +20,7 @@ class Container21(containers.DeclarativeContainer):
@containers.override(Container21)
class Container22(containers.DeclarativeContainer):
...
class Container22(containers.DeclarativeContainer): ...
# Test 3: to check @copy decorator
@ -30,14 +29,14 @@ class Container31(containers.DeclarativeContainer):
@containers.copy(Container31)
class Container32(containers.DeclarativeContainer):
...
class Container32(containers.DeclarativeContainer): ...
# Test 4: to override()
class Container4(containers.DeclarativeContainer):
provider = providers.Factory(int)
container4 = Container4()
container4.override(Container4())
@ -47,7 +46,7 @@ class Container5(containers.DeclarativeContainer):
provider = providers.Factory(int)
dependencies: Dict[str, providers.Provider] = Container5.dependencies
dependencies: Dict[str, providers.Provider[Any]] = Container5.dependencies
# Test 6: to check base class
@ -62,6 +61,7 @@ container6: containers.Container = Container6()
class Container7(containers.DeclarativeContainer):
provider = providers.Factory(str)
container7 = Container7()
container7.override_providers(provider="new_value")
with container7.override_providers(a=providers.Provider()):

View File

@ -1,17 +1,20 @@
from typing import Optional
from typing import Any, Optional
from dependency_injector import providers
# Test 1: to check the return type
provider1 = providers.Delegate(providers.Provider())
var1: providers.Provider = provider1()
var1: providers.Provider[Any] = provider1()
# Test 2: to check the return type with await
provider2 = providers.Delegate(providers.Provider())
async def _async2() -> None:
var1: providers.Provider = await provider2() # type: ignore
var2: providers.Provider = await provider2.async_()
var1: providers.Provider[Any] = await provider2() # type: ignore
var2: providers.Provider[Any] = await provider2.async_()
# Test 3: to check class type from provider
provider3 = providers.Delegate(providers.Provider())
provided_provides: Optional[providers.Provider] = provider3.provides
provided_provides: Optional[providers.Provider[Any]] = provider3.provides

View File

@ -1,11 +1,12 @@
from dependency_injector import providers
from typing import Any
from dependency_injector import providers
# Test 1: to check the getattr type
provider1 = providers.DependenciesContainer(
a=providers.Provider(),
b=providers.Provider(),
)
a1: providers.Provider = provider1.a
b1: providers.Provider = provider1.b
a1: providers.Provider[Any] = provider1.a
b1: providers.Provider[Any] = provider1.b
c1: providers.ProvidedInstance = provider1.c.provided

View File

@ -1,15 +1,14 @@
from typing import Type
from typing import Any, Type
from dependency_injector import providers
class Animal:
...
class Animal: ...
class Cat(Animal):
def __init__(self, *_, **__): ...
def __init__(self, *a: Any, **kw: Any) -> None: ...
# Test 1: to check the return type
@ -23,6 +22,8 @@ var2: Type[Animal] = provider2.instance_of
# Test 3: to check the return type with await
provider3 = providers.Dependency(instance_of=Animal)
async def _async3() -> None:
var1: Animal = await provider3() # type: ignore
var2: Animal = await provider3.async_()

View File

@ -2,7 +2,6 @@ from typing import Any, Dict
from dependency_injector import providers
# Test 1: to check the return type (class)
provider1 = providers.Dict(
a1=providers.Factory(object),
@ -17,7 +16,9 @@ var2: Dict[Any, Any] = provider2()
# Test 3: to check init with non-string keys
provider3 = providers.Dict({object(): providers.Factory(object)}, a2=providers.Factory(object))
provider3 = providers.Dict(
{object(): providers.Factory(object)}, a2=providers.Factory(object)
)
var3: Dict[Any, Any] = provider3()
@ -42,6 +43,8 @@ provider6 = providers.Dict(
a1=providers.Factory(object),
a2=providers.Factory(object),
)
async def _async3() -> None:
var1: Dict[Any, Any] = await provider6() # type: ignore
var2: Dict[Any, Any] = await provider6.async_()

View File

@ -1,8 +1,7 @@
from typing import Dict
from typing import Any, Dict
from dependency_injector import containers, providers
# Test 1: to check setattr
container1 = containers.DynamicContainer()
container1.abc = providers.Provider()
@ -23,7 +22,7 @@ container4.set_providers(a=providers.Provider())
# Test 5: to check .dependencies attribute
container5 = containers.DynamicContainer()
dependencies: Dict[str, providers.Provider] = container5.dependencies
dependencies: Dict[str, providers.Provider[Any]] = container5.dependencies
# Test 6: to check base class
container6: containers.Container = containers.DynamicContainer()

View File

@ -1,15 +1,14 @@
from typing import Callable, Optional, Tuple, Any, Dict, Type
from typing import Any, Callable, Dict, Optional, Tuple, Type
from dependency_injector import providers
class Animal:
...
class Animal: ...
class Cat(Animal):
def __init__(self, *_, **__): ...
def __init__(self, *a: Any, **kw: Any) -> None: ...
@classmethod
def create(cls) -> Animal:
@ -63,17 +62,29 @@ factory_a_9: providers.Factory[str] = provider9.a
factory_b_9: providers.Factory[str] = provider9.b
val9: str = provider9("a")
provider9_set_non_string_keys: providers.FactoryAggregate[str] = providers.FactoryAggregate()
provider9_set_non_string_keys: providers.FactoryAggregate[str] = (
providers.FactoryAggregate()
)
provider9_set_non_string_keys.set_factories({Cat: providers.Factory(str, "str")})
factory_set_non_string_9: providers.Factory[str] = provider9_set_non_string_keys.factories[Cat]
factory_set_non_string_9: providers.Factory[str] = (
provider9_set_non_string_keys.factories[Cat]
)
provider9_new_non_string_keys: providers.FactoryAggregate[str] = providers.FactoryAggregate(
provider9_new_non_string_keys: providers.FactoryAggregate[str] = (
providers.FactoryAggregate(
{Cat: providers.Factory(str, "str")},
)
factory_new_non_string_9: providers.Factory[str] = provider9_new_non_string_keys.factories[Cat]
)
factory_new_non_string_9: providers.Factory[str] = (
provider9_new_non_string_keys.factories[Cat]
)
provider9_no_explicit_typing = providers.FactoryAggregate(a=providers.Factory(str, "str"))
provider9_no_explicit_typing_factory: providers.Factory[str] = provider9_no_explicit_typing.factories["a"]
provider9_no_explicit_typing = providers.FactoryAggregate(
a=providers.Factory(str, "str")
)
provider9_no_explicit_typing_factory: providers.Factory[str] = (
provider9_no_explicit_typing.factories["a"]
)
provider9_no_explicit_typing_object: str = provider9_no_explicit_typing("a")
# Test 10: to check the explicit typing
@ -82,10 +93,13 @@ animal10: Animal = factory10()
# Test 11: to check the return type with await
provider11 = providers.Factory(Cat)
async def _async11() -> None:
animal1: Animal = await provider11(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
animal2: Animal = await provider11.async_(1, 2, 3, b="1", c=2, e=0.0)
# Test 12: to check class type from .provides
provider12 = providers.Factory(Cat)
provided_cls12: Type[Animal] = provider12.cls
@ -101,5 +115,5 @@ provided_provides13: Optional[Callable[..., Animal]] = provider13.provides
assert provided_provides13 is not None and provided_provides13() == Cat()
# Test 14: to check string imports
provider14: providers.Factory[dict] = providers.Factory("builtins.dict")
provider14: providers.Factory[Dict[Any, Any]] = providers.Factory("builtins.dict")
provider14.set_provides("builtins.dict")

View File

@ -1,8 +1,7 @@
from typing import Tuple, Any, List
from typing import Any, List, Tuple
from dependency_injector import providers
# Test 1: to check the return type (class)
provider1 = providers.List(
providers.Factory(object),
@ -33,6 +32,8 @@ provider4 = providers.List(
providers.Factory(object),
providers.Factory(object),
)
async def _async4() -> None:
var1: List[Any] = await provider4() # type: ignore
var2: List[Any] = await provider4.async_()

View File

@ -1,8 +1,7 @@
from typing import Type, Optional
from typing import Optional, Type
from dependency_injector import providers
# Test 1: to check the return type
provider1 = providers.Object(int(3))
var1: int = provider1()
@ -16,10 +15,13 @@ method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg
# Test 3: to check the return type with await
provider3 = providers.Object(int(3))
async def _async3() -> None:
var1: int = await provider3() # type: ignore
var2: int = await provider3.async_()
# Test 4: to check class type from provider
provider4 = providers.Object(int("1"))
provided_provides: Optional[int] = provider4.provides

View File

@ -1,5 +1,6 @@
from dependency_injector import providers
from typing import Any
from dependency_injector import providers
# Test 1: to check .provided attribute
provider1: providers.Provider[int] = providers.Object(1)
@ -7,7 +8,7 @@ provided: int = provider1.provided()
provider1_delegate: providers.Provider[int] = provider1.provider
# Test 2: to check async mode API
provider2: providers.Provider = providers.Provider()
provider2: providers.Provider[Any] = providers.Provider()
provider2.enable_async_mode()
provider2.disable_async_mode()
provider2.reset_async_mode()

View File

@ -1,4 +1,13 @@
from typing import List, Iterator, Generator, AsyncIterator, AsyncGenerator, Optional
from typing import (
Any,
AsyncGenerator,
AsyncIterator,
Dict,
Generator,
Iterator,
List,
Optional,
)
from dependency_injector import providers, resources
@ -32,11 +41,10 @@ var3: List[int] = provider3()
# Test 4: to check the return type with resource subclass
class MyResource4(resources.Resource[List[int]]):
def init(self, *args, **kwargs) -> List[int]:
def init(self, *args: Any, **kwargs: Any) -> List[int]:
return []
def shutdown(self, resource: Optional[List[int]]) -> None:
...
def shutdown(self, resource: Optional[List[int]]) -> None: ...
provider4 = providers.Resource(MyResource4)
@ -84,11 +92,10 @@ async def _provide7() -> None:
# Test 8: to check the return type with async resource subclass
class MyResource8(resources.AsyncResource[List[int]]):
async def init(self, *args, **kwargs) -> List[int]:
async def init(self, *args: Any, **kwargs: Any) -> List[int]:
return []
async def shutdown(self, resource: Optional[List[int]]) -> None:
...
async def shutdown(self, resource: Optional[List[int]]) -> None: ...
provider8 = providers.Resource(MyResource8)
@ -100,5 +107,5 @@ async def _provide8() -> None:
# Test 9: to check string imports
provider9: providers.Resource[dict] = providers.Resource("builtins.dict")
provider9: providers.Resource[Dict[Any, Any]] = providers.Resource("builtins.dict")
provider9.set_provides("builtins.dict")

View File

@ -2,7 +2,6 @@ from typing import Any
from dependency_injector import providers
# Test 1: to check the return type
provider1 = providers.Selector(
lambda: "a",
@ -28,7 +27,7 @@ provider3 = providers.Selector(
a=providers.Factory(object),
b=providers.Factory(object),
)
attr3: providers.Provider = provider3.a
attr3: providers.Provider[Any] = provider3.a
# Test 4: to check the return type with await
provider4 = providers.Selector(
@ -36,6 +35,8 @@ provider4 = providers.Selector(
a=providers.Factory(object),
b=providers.Factory(object),
)
async def _async4() -> None:
var1: Any = await provider4() # type: ignore
var1: Any = await provider4()
var2: Any = await provider4.async_()

View File

@ -1,15 +1,14 @@
from typing import Callable, Optional, Tuple, Any, Dict, Type
from typing import Any, Callable, Dict, Optional, Tuple, Type
from dependency_injector import providers
class Animal:
...
class Animal: ...
class Cat(Animal):
def __init__(self, *_, **__): ...
def __init__(self, *a: Any, **kw: Any) -> None: ...
@classmethod
def create(cls) -> Animal:
@ -72,10 +71,13 @@ provider12 = providers.SingletonDelegate(providers.Singleton(object))
# Test 13: to check the return type with await
provider13 = providers.Singleton(Cat)
async def _async13() -> None:
animal1: Animal = await provider13(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
animal2: Animal = await provider13.async_(1, 2, 3, b="1", c=2, e=0.0)
# Test 14: to check class from .provides
provider14 = providers.Singleton(Cat)
provided_cls14: Type[Cat] = provider14.cls
@ -91,5 +93,5 @@ provided_provides15: Optional[Callable[..., Animal]] = provider15.provides
assert provided_provides15 is not None and provided_provides15() == Cat()
# Test 16: to check string imports
provider16: providers.Singleton[dict] = providers.Singleton("builtins.dict")
provider16: providers.Singleton[Dict[Any, Any]] = providers.Singleton("builtins.dict")
provider16.set_provides("builtins.dict")

View File

@ -88,4 +88,4 @@ deps=
pydantic-settings
mypy
commands=
mypy tests/typing
mypy --strict tests/typing