1
1
import sys
2
- from typing import Any
2
+ from typing import Any , Type
3
3
4
4
if sys .version_info >= (3 , 11 ): # pragma: no cover
5
5
from typing import Self
6
6
else : # pragma: no cover
7
7
from typing_extensions import Self
8
8
9
9
from dependency_injector .containers import Container
10
+ from dependency_injector .providers import Resource
10
11
11
12
12
13
class Lifespan :
@@ -29,24 +30,32 @@ class Container(DeclarativeContainer):
29
30
app = Factory(Starlette, lifespan=lifespan)
30
31
31
32
:param container: container instance
33
+ :param resource_type: A :py:class:`~dependency_injector.resources.Resource`
34
+ subclass. Limits the resources to be initialized and shutdown.
32
35
"""
33
36
34
37
container : Container
38
+ resource_type : Type [Resource [Any ]]
35
39
36
- def __init__ (self , container : Container ) -> None :
40
+ def __init__ (
41
+ self ,
42
+ container : Container ,
43
+ resource_type : Type [Resource [Any ]] = Resource ,
44
+ ) -> None :
37
45
self .container = container
46
+ self .resource_type = resource_type
38
47
39
48
def __call__ (self , app : Any ) -> Self :
40
49
return self
41
50
42
51
async def __aenter__ (self ) -> None :
43
- result = self .container .init_resources ()
52
+ result = self .container .init_resources (self . resource_type )
44
53
45
54
if result is not None :
46
55
await result
47
56
48
57
async def __aexit__ (self , * exc_info : Any ) -> None :
49
- result = self .container .shutdown_resources ()
58
+ result = self .container .shutdown_resources (self . resource_type )
50
59
51
60
if result is not None :
52
61
await result
0 commit comments