11import sys
2- from typing import Any
2+ from typing import Any , Type
33
44if sys .version_info >= (3 , 11 ): # pragma: no cover
55 from typing import Self
66else : # pragma: no cover
77 from typing_extensions import Self
88
99from dependency_injector .containers import Container
10+ from dependency_injector .providers import Resource
1011
1112
1213class Lifespan :
@@ -29,24 +30,32 @@ class Container(DeclarativeContainer):
2930 app = Factory(Starlette, lifespan=lifespan)
3031
3132 :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.
3235 """
3336
3437 container : Container
38+ resource_type : Type [Resource [Any ]]
3539
36- def __init__ (self , container : Container ) -> None :
40+ def __init__ (
41+ self ,
42+ container : Container ,
43+ resource_type : Type [Resource [Any ]] = Resource ,
44+ ) -> None :
3745 self .container = container
46+ self .resource_type = resource_type
3847
3948 def __call__ (self , app : Any ) -> Self :
4049 return self
4150
4251 async def __aenter__ (self ) -> None :
43- result = self .container .init_resources ()
52+ result = self .container .init_resources (self . resource_type )
4453
4554 if result is not None :
4655 await result
4756
4857 async def __aexit__ (self , * exc_info : Any ) -> None :
49- result = self .container .shutdown_resources ()
58+ result = self .container .shutdown_resources (self . resource_type )
5059
5160 if result is not None :
5261 await result
0 commit comments