Skip to content

Commit 00606b1

Browse files
Merge 41ca172 into abc79ef
2 parents abc79ef + 41ca172 commit 00606b1

File tree

10 files changed

+84
-39
lines changed

10 files changed

+84
-39
lines changed

src/dependency_injector/providers.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class AbstractCallable(Callable[T]):
197197
class CallableDelegate(Delegate):
198198
def __init__(self, callable: Callable) -> None: ...
199199

200-
class Coroutine(Callable[T]): ...
200+
class Coroutine(Callable[T_Any]): ...
201201
class DelegatedCoroutine(Coroutine[T]): ...
202202

203203
class AbstractCoroutine(Coroutine[T]):

tests/typing/configuration.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from pathlib import Path
22
from typing import Any, Dict
3+
from typing_extensions import assert_type
34

45
from pydantic_settings import BaseSettings as PydanticSettings
56

67
from dependency_injector import providers
78

89
# Test 1: to check the getattr
9-
config1: providers.Configuration = providers.Configuration()
10-
provider1: providers.Provider[Dict[str, Any]] = providers.Factory(dict, a=config1.a)
10+
config1 = providers.Configuration()
11+
provider1 = providers.Factory(dict[str, Any], a=config1.a)
12+
assert_type(provider1, providers.Factory[Dict[str, Any]])
1113

1214
# Test 2: to check the from_*() method
1315
config2 = providers.Configuration()
@@ -33,44 +35,53 @@
3335

3436
# Test 3: to check as_*() methods
3537
config3 = providers.Configuration()
36-
int3: providers.Callable[int] = config3.option.as_int()
37-
float3: providers.Callable[float] = config3.option.as_float()
38-
int3_custom: providers.Callable[int] = config3.option.as_(int)
38+
int3 = config3.option.as_int()
39+
float3 = config3.option.as_float()
40+
int3_custom = config3.option.as_(int)
41+
42+
assert_type(int3, providers.TypedConfigurationOption[int])
43+
assert_type(float3, providers.TypedConfigurationOption[float])
44+
assert_type(int3_custom, providers.TypedConfigurationOption[int])
3945

4046
# Test 4: to check required() method
4147
config4 = providers.Configuration()
42-
option4: providers.ConfigurationOption = config4.option.required()
48+
option4 = config4.option.required()
49+
assert_type(option4, providers.ConfigurationOption)
50+
4351

4452
# Test 5: to check get/set config files' methods and init arguments
4553
# Test 5: ini
4654
config5_ini = providers.Configuration(
4755
ini_files=["config.ini", Path("config.ini")],
4856
)
4957
config5_ini.set_ini_files(["config.ini", Path("config.ini")])
50-
config5_ini_files: list[str | Path] = config5_ini.get_ini_files()
58+
config5_ini_files = config5_ini.get_ini_files()
59+
assert_type(config5_ini_files, list[str | Path])
5160

5261
# Test 5: yaml
5362
config5_yaml = providers.Configuration(
5463
yaml_files=["config.yml", Path("config.yml")],
5564
)
5665
config5_yaml.set_yaml_files(["config.yml", Path("config.yml")])
5766
config5_yaml_files: list[str | Path] = config5_yaml.get_yaml_files()
67+
assert_type(config5_yaml_files, list[str | Path])
5868

5969
# Test 5: json
6070
config5_json = providers.Configuration(
6171
json_files=["config.json", Path("config.json")],
6272
)
6373
config5_json.set_json_files(["config.json", Path("config.json")])
64-
config5_json_files: list[str | Path] = config5_json.get_json_files()
74+
config5_json_files = config5_json.get_json_files()
75+
assert_type(config5_json_files, list[str | Path])
6576

6677
# Test 5: pydantic
6778
config5_pydantic = providers.Configuration(
6879
pydantic_settings=[PydanticSettings()],
6980
)
7081
config5_pydantic.set_pydantic_settings([PydanticSettings()])
71-
config5_pydantic_settings: list[PydanticSettings] = (
72-
config5_pydantic.get_pydantic_settings()
73-
)
82+
83+
# NOTE: Using assignment since PydanticSettings is context-sensitive: conditional on whether pydantic is installed
84+
config5_pydantic_settings: list[PydanticSettings] = (config5_pydantic.get_pydantic_settings())
7485

7586
# Test 6: to check init arguments
7687
config6 = providers.Configuration(

tests/typing/container.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any
2+
from typing_extensions import assert_type
23

34
from dependency_injector import providers
45

@@ -8,8 +9,11 @@ class Container: ...
89

910
# Test 1: to check the return type
1011
provider1 = providers.Container(Container)
11-
var1: Container = provider1()
12+
var1 = provider1()
13+
assert_type(var1, Container)
14+
1215

1316
# Test 2: to check the getattr
1417
provider2 = providers.Container(Container)
15-
attr: providers.Provider[Any] = provider2.attr
18+
attr = provider2.attr
19+
assert_type(attr, providers.Provider[Any])

tests/typing/coroutine.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Awaitable, Coroutine
1+
from typing import Awaitable, Coroutine, Any
2+
from typing_extensions import assert_type
23

34
from dependency_injector import providers
45

@@ -8,8 +9,10 @@ async def _coro() -> None: ...
89

910
# Test 1: to check the return type
1011
provider1 = providers.Coroutine(_coro)
11-
var1: Awaitable[None] = provider1()
12+
var1 = provider1()
13+
assert_type(var1, Coroutine[Any, Any, None]) # type: ignore[unused-coroutine]
1214

1315
# Test 2: to check string imports
14-
provider2: providers.Coroutine[None] = providers.Coroutine("_coro")
16+
provider2 = providers.Coroutine("_coro")
1517
provider2.set_provides("_coro")
18+
assert_type(provider2, providers.Coroutine[Any])

tests/typing/declarative_container.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Dict
2+
from typing_extensions import assert_type
23

34
from dependency_injector import containers, providers
45

@@ -8,10 +9,12 @@ class Container1(containers.DeclarativeContainer):
89
provider = providers.Factory(int)
910

1011

12+
# NOTE: Using assignment to check base class instead of exact type
1113
container1 = Container1()
1214
container1_type: containers.Container = Container1()
1315
provider1: providers.Provider[int] = container1.provider
14-
val1: int = container1.provider(3)
16+
val1 = container1.provider(3)
17+
assert_type(val1, int)
1518

1619

1720
# Test 2: to check @override decorator
@@ -46,8 +49,8 @@ class Container5(containers.DeclarativeContainer):
4649
provider = providers.Factory(int)
4750

4851

49-
dependencies: Dict[str, providers.Provider[Any]] = Container5.dependencies
50-
52+
dependencies = Container5.dependencies
53+
assert_type(dependencies, Dict[str, providers.Provider[Any]])
5154

5255
# Test 6: to check base class
5356
class Container6(containers.DeclarativeContainer):

tests/typing/delegate.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
from typing import Any, Optional
2+
from typing_extensions import assert_type
23

34
from dependency_injector import providers
45

56
# Test 1: to check the return type
67
provider1 = providers.Delegate(providers.Provider())
7-
var1: providers.Provider[Any] = provider1()
8+
var1 = provider1()
9+
assert_type(var1, providers.Provider[Any])
810

911
# Test 2: to check the return type with await
1012
provider2 = providers.Delegate(providers.Provider())
1113

1214

1315
async def _async2() -> None:
14-
var1: providers.Provider[Any] = await provider2() # type: ignore
15-
var2: providers.Provider[Any] = await provider2.async_()
16+
var1 = await provider2() # type: ignore
17+
var2 = await provider2.async_()
18+
assert_type(var2, providers.Provider[Any])
1619

1720

1821
# Test 3: to check class type from provider
1922
provider3 = providers.Delegate(providers.Provider())
20-
provided_provides: Optional[providers.Provider[Any]] = provider3.provides
23+
provided_provides = provider3.provides
24+
assert_type(provided_provides, Optional[providers.Provider[Any]])
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any
2+
from typing_extensions import assert_type
23

34
from dependency_injector import providers
45

@@ -7,6 +8,10 @@
78
a=providers.Provider(),
89
b=providers.Provider(),
910
)
10-
a1: providers.Provider[Any] = provider1.a
11-
b1: providers.Provider[Any] = provider1.b
12-
c1: providers.ProvidedInstance = provider1.c.provided
11+
a1 = provider1.a
12+
b1 = provider1.b
13+
c1 = provider1.c.provided
14+
15+
assert_type(a1, providers.Provider[Any])
16+
assert_type(b1, providers.Provider[Any])
17+
assert_type(c1, providers.ProvidedInstance)

tests/typing/dependency.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Type
2+
from typing_extensions import assert_type
23

34
from dependency_injector import providers
45

@@ -14,16 +15,19 @@ def __init__(self, *a: Any, **kw: Any) -> None: ...
1415
# Test 1: to check the return type
1516
provider1 = providers.Dependency(instance_of=Animal)
1617
provider1.override(providers.Factory(Cat))
17-
var1: Animal = provider1()
18+
var1 = provider1()
19+
assert_type(var1, Animal)
1820

1921
# Test 2: to check the return type
2022
provider2 = providers.Dependency(instance_of=Animal)
21-
var2: Type[Animal] = provider2.instance_of
23+
var2 = provider2.instance_of
24+
assert_type(var2, Type[Animal])
2225

2326
# Test 3: to check the return type with await
2427
provider3 = providers.Dependency(instance_of=Animal)
2528

2629

2730
async def _async3() -> None:
28-
var1: Animal = await provider3() # type: ignore
29-
var2: Animal = await provider3.async_()
31+
var1 = await provider3() # type: ignore
32+
var2 = await provider3.async_()
33+
assert_type(var2, Animal)

tests/typing/dict.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Dict
2+
from typing_extensions import assert_type
23

34
from dependency_injector import providers
45

@@ -7,35 +8,41 @@
78
a1=providers.Factory(object),
89
a2=providers.Factory(object),
910
)
10-
var1: Dict[Any, Any] = provider1()
11+
var1 = provider1()
12+
assert_type(var1, Dict[Any, Any])
13+
1114

1215

1316
# Test 2: to check init with non-string keys
1417
provider2 = providers.Dict({object(): providers.Factory(object)})
15-
var2: Dict[Any, Any] = provider2()
18+
var2 = provider2()
19+
assert_type(var2, Dict[Any, Any])
1620

1721

1822
# Test 3: to check init with non-string keys
1923
provider3 = providers.Dict(
2024
{object(): providers.Factory(object)}, a2=providers.Factory(object)
2125
)
22-
var3: Dict[Any, Any] = provider3()
26+
var3 = provider3()
27+
assert_type(var3, Dict[Any, Any])
2328

2429

2530
# Test 4: to check the .args attributes
2631
provider4 = providers.Dict(
2732
a1=providers.Factory(object),
2833
a2=providers.Factory(object),
2934
)
30-
args4: Dict[Any, Any] = provider4.kwargs
35+
args = provider4.kwargs
36+
assert_type(args, Dict[Any, Any])
3137

3238

3339
# Test 5: to check the provided instance interface
3440
provider5 = providers.Dict(
3541
a1=providers.Factory(object),
3642
a2=providers.Factory(object),
3743
)
38-
provided5: dict[Any, Any] = provider5.provided()
44+
provided5 = provider5.provided()
45+
assert_type(provided5, Any)
3946

4047

4148
# Test 6: to check the return type with await
@@ -46,5 +53,6 @@
4653

4754

4855
async def _async3() -> None:
49-
var1: Dict[Any, Any] = await provider6() # type: ignore
50-
var2: Dict[Any, Any] = await provider6.async_()
56+
var1 = await provider6() # type: ignore
57+
var2 = await provider6.async_()
58+
assert_type(var2, Dict[Any, Any])

tests/typing/dynamic_container.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Dict
2+
from typing_extensions import assert_type
23

34
from dependency_injector import containers, providers
45

@@ -22,7 +23,9 @@
2223

2324
# Test 5: to check .dependencies attribute
2425
container5 = containers.DynamicContainer()
25-
dependencies: Dict[str, providers.Provider[Any]] = container5.dependencies
26+
dependencies = container5.dependencies
27+
assert_type(dependencies, Dict[str, providers.Provider[Any]])
2628

2729
# Test 6: to check base class
30+
# NOTE: Using assignment to check base class instead of exact type
2831
container6: containers.Container = containers.DynamicContainer()

0 commit comments

Comments
 (0)