Skip to content

Commit 7fcf1ac

Browse files
committed
Make mypy --strict tests/typing passable
1 parent 1271d0f commit 7fcf1ac

19 files changed

+125
-81
lines changed

tests/typing/aggregate.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from dependency_injector import providers
22

33

4-
class Animal:
5-
...
4+
class Animal: ...
65

76

8-
class Cat(Animal):
9-
...
7+
class Cat(Animal): ...
108

119

1210
# Test 1: to check Aggregate provider
@@ -20,13 +18,19 @@ class Cat(Animal):
2018

2119
provider1_set_non_string_keys: providers.Aggregate[str] = providers.Aggregate()
2220
provider1_set_non_string_keys.set_providers({Cat: providers.Object("str")})
23-
provider_set_non_string_1: providers.Provider[str] = provider1_set_non_string_keys.providers[Cat]
21+
provider_set_non_string_1: providers.Provider[str] = (
22+
provider1_set_non_string_keys.providers[Cat]
23+
)
2424

2525
provider1_new_non_string_keys: providers.Aggregate[str] = providers.Aggregate(
2626
{Cat: providers.Object("str")},
2727
)
28-
factory_new_non_string_1: providers.Provider[str] = provider1_new_non_string_keys.providers[Cat]
28+
factory_new_non_string_1: providers.Provider[str] = (
29+
provider1_new_non_string_keys.providers[Cat]
30+
)
2931

3032
provider1_no_explicit_typing = providers.Aggregate(a=providers.Object("str"))
31-
provider1_no_explicit_typing_factory: providers.Provider[str] = provider1_no_explicit_typing.providers["a"]
33+
provider1_no_explicit_typing_factory: providers.Provider[str] = (
34+
provider1_no_explicit_typing.providers["a"]
35+
)
3236
provider1_no_explicit_typing_object: str = provider1_no_explicit_typing("a")

tests/typing/callable.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from typing import Callable, Optional, Tuple, Any, Dict, Type
1+
from typing import Any, Callable, Dict, Optional, Tuple, Type
22

33
from dependency_injector import providers
44

55

6-
class Animal:
7-
...
6+
class Animal: ...
87

98

109
class Cat(Animal):
@@ -53,10 +52,13 @@ def create(cls) -> Animal:
5352

5453
# Test 9: to check the return type with await
5554
provider9 = providers.Callable(Cat)
55+
56+
5657
async def _async9() -> None:
5758
animal1: Animal = await provider9(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
5859
animal2: Animal = await provider9.async_(1, 2, 3, b="1", c=2, e=0.0)
5960

61+
6062
# Test 10: to check the .provides
6163
provider10 = providers.Callable(Cat)
6264
provides10: Optional[Callable[..., Cat]] = provider10.provides
@@ -68,5 +70,5 @@ async def _async9() -> None:
6870
assert provides11 is Cat
6971

7072
# Test 12: to check string imports
71-
provider12: providers.Callable[dict] = providers.Callable("builtins.dict")
73+
provider12: providers.Callable[Dict[Any, Any]] = providers.Callable("builtins.dict")
7274
provider12.set_provides("builtins.dict")

tests/typing/configuration.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from pathlib import Path
2-
from typing import Any
2+
from typing import Any, Dict
33

4-
from dependency_injector import providers
54
from pydantic_settings import BaseSettings as PydanticSettings
65

6+
from dependency_injector import providers
77

88
# Test 1: to check the getattr
99
config1: providers.Configuration = providers.Configuration()
10-
provider1: providers.Provider[dict] = providers.Factory(dict, a=config1.a)
10+
provider1: providers.Provider[Dict[str, Any]] = providers.Factory(dict, a=config1.a)
1111

1212
# Test 2: to check the from_*() method
1313
config2 = providers.Configuration()
@@ -68,7 +68,9 @@
6868
pydantic_settings=[PydanticSettings()],
6969
)
7070
config5_pydantic.set_pydantic_settings([PydanticSettings()])
71-
config5_pydantic_settings: list[PydanticSettings] = config5_pydantic.get_pydantic_settings()
71+
config5_pydantic_settings: list[PydanticSettings] = (
72+
config5_pydantic.get_pydantic_settings()
73+
)
7274

7375
# Test 6: to check init arguments
7476
config6 = providers.Configuration(

tests/typing/container.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from typing import Any
2+
13
from dependency_injector import providers
24

35

4-
class Container:
5-
...
6+
class Container: ...
67

78

89
# Test 1: to check the return type
@@ -11,4 +12,4 @@ class Container:
1112

1213
# Test 2: to check the getattr
1314
provider2 = providers.Container(Container)
14-
attr: providers.Provider = provider2.attr
15+
attr: providers.Provider[Any] = provider2.attr

tests/typing/coroutine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from typing import Coroutine
1+
from typing import Awaitable, Coroutine
22

33
from dependency_injector import providers
44

55

6-
async def _coro() -> None:
7-
...
6+
async def _coro() -> None: ...
7+
88

99
# Test 1: to check the return type
1010
provider1 = providers.Coroutine(_coro)
11-
var1: Coroutine = provider1()
11+
var1: Awaitable[None] = provider1()
1212

1313
# Test 2: to check string imports
1414
provider2: providers.Coroutine[None] = providers.Coroutine("_coro")

tests/typing/declarative_container.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict
1+
from typing import Any, Dict
22

33
from dependency_injector import containers, providers
44

@@ -10,7 +10,7 @@ class Container1(containers.DeclarativeContainer):
1010

1111
container1 = Container1()
1212
container1_type: containers.Container = Container1()
13-
provider1: providers.Provider = container1.provider
13+
provider1: providers.Provider[int] = container1.provider
1414
val1: int = container1.provider(3)
1515

1616

@@ -20,8 +20,7 @@ class Container21(containers.DeclarativeContainer):
2020

2121

2222
@containers.override(Container21)
23-
class Container22(containers.DeclarativeContainer):
24-
...
23+
class Container22(containers.DeclarativeContainer): ...
2524

2625

2726
# Test 3: to check @copy decorator
@@ -30,14 +29,14 @@ class Container31(containers.DeclarativeContainer):
3029

3130

3231
@containers.copy(Container31)
33-
class Container32(containers.DeclarativeContainer):
34-
...
32+
class Container32(containers.DeclarativeContainer): ...
3533

3634

3735
# Test 4: to override()
3836
class Container4(containers.DeclarativeContainer):
3937
provider = providers.Factory(int)
4038

39+
4140
container4 = Container4()
4241
container4.override(Container4())
4342

@@ -47,7 +46,7 @@ class Container5(containers.DeclarativeContainer):
4746
provider = providers.Factory(int)
4847

4948

50-
dependencies: Dict[str, providers.Provider] = Container5.dependencies
49+
dependencies: Dict[str, providers.Provider[Any]] = Container5.dependencies
5150

5251

5352
# Test 6: to check base class
@@ -62,6 +61,7 @@ class Container6(containers.DeclarativeContainer):
6261
class Container7(containers.DeclarativeContainer):
6362
provider = providers.Factory(str)
6463

64+
6565
container7 = Container7()
6666
container7.override_providers(provider="new_value")
6767
with container7.override_providers(a=providers.Provider()):

tests/typing/delegate.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
from typing import Optional
1+
from typing import Any, Optional
22

33
from dependency_injector import providers
44

55
# Test 1: to check the return type
66
provider1 = providers.Delegate(providers.Provider())
7-
var1: providers.Provider = provider1()
7+
var1: providers.Provider[Any] = provider1()
88

99
# Test 2: to check the return type with await
1010
provider2 = providers.Delegate(providers.Provider())
11+
12+
1113
async def _async2() -> None:
12-
var1: providers.Provider = await provider2() # type: ignore
13-
var2: providers.Provider = await provider2.async_()
14+
var1: providers.Provider[Any] = await provider2() # type: ignore
15+
var2: providers.Provider[Any] = await provider2.async_()
16+
1417

1518
# Test 3: to check class type from provider
1619
provider3 = providers.Delegate(providers.Provider())
17-
provided_provides: Optional[providers.Provider] = provider3.provides
20+
provided_provides: Optional[providers.Provider[Any]] = provider3.provides
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from dependency_injector import providers
1+
from typing import Any
22

3+
from dependency_injector import providers
34

45
# Test 1: to check the getattr type
56
provider1 = providers.DependenciesContainer(
67
a=providers.Provider(),
78
b=providers.Provider(),
89
)
9-
a1: providers.Provider = provider1.a
10-
b1: providers.Provider = provider1.b
10+
a1: providers.Provider[Any] = provider1.a
11+
b1: providers.Provider[Any] = provider1.b
1112
c1: providers.ProvidedInstance = provider1.c.provided

tests/typing/dependency.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from typing import Type
1+
from typing import Any, Type
22

33
from dependency_injector import providers
44

55

6-
class Animal:
7-
...
6+
class Animal: ...
87

98

109
class Cat(Animal):
1110

12-
def __init__(self, *_, **__): ...
11+
def __init__(self, *a: Any, **kw: Any) -> None: ...
1312

1413

1514
# Test 1: to check the return type
@@ -23,6 +22,8 @@ def __init__(self, *_, **__): ...
2322

2423
# Test 3: to check the return type with await
2524
provider3 = providers.Dependency(instance_of=Animal)
25+
26+
2627
async def _async3() -> None:
2728
var1: Animal = await provider3() # type: ignore
2829
var2: Animal = await provider3.async_()

tests/typing/dict.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from dependency_injector import providers
44

5-
65
# Test 1: to check the return type (class)
76
provider1 = providers.Dict(
87
a1=providers.Factory(object),
@@ -17,7 +16,9 @@
1716

1817

1918
# Test 3: to check init with non-string keys
20-
provider3 = providers.Dict({object(): providers.Factory(object)}, a2=providers.Factory(object))
19+
provider3 = providers.Dict(
20+
{object(): providers.Factory(object)}, a2=providers.Factory(object)
21+
)
2122
var3: Dict[Any, Any] = provider3()
2223

2324

@@ -42,6 +43,8 @@
4243
a1=providers.Factory(object),
4344
a2=providers.Factory(object),
4445
)
46+
47+
4548
async def _async3() -> None:
4649
var1: Dict[Any, Any] = await provider6() # type: ignore
4750
var2: Dict[Any, Any] = await provider6.async_()

0 commit comments

Comments
 (0)