Allow Closing to detect dependent resources passed as kwargs too #636

This commit is contained in:
federinik 2022-12-21 19:14:06 +01:00
parent cc2304e46e
commit 511834034d
3 changed files with 21 additions and 2 deletions

View File

@ -598,7 +598,7 @@ def _locate_dependent_closing_args(provider: providers.Provider) -> Dict[str, pr
return {}
closing_deps = {}
for arg in provider.args:
for arg in [*provider.args, *provider.kwargs.values()]:
if not isinstance(arg, providers.Provider) or not hasattr(arg, "args"):
continue

View File

@ -36,6 +36,7 @@ class Container(containers.DeclarativeContainer):
service = providers.Resource(init_service)
factory_service = providers.Factory(FactoryService, service)
factory_service_kwargs = providers.Factory(FactoryService, service=service)
@inject
@ -46,3 +47,8 @@ def test_function(service: Service = Closing[Provide["service"]]):
@inject
def test_function_dependency(factory: FactoryService = Closing[Provide["factory_service"]]):
return factory
@inject
def test_function_dependency_kwargs(factory: FactoryService = Closing[Provide["factory_service_kwargs"]]):
return factory

View File

@ -303,7 +303,20 @@ def test_closing_dependency_resource():
assert result_2.service.init_counter == 2
assert result_2.service.shutdown_counter == 2
assert result_1 is not result_2
@mark.usefixtures("resourceclosing_container")
def test_closing_dependency_resource_kwargs():
resourceclosing.Service.reset_counter()
result_1 = resourceclosing.test_function_dependency_kwargs()
assert isinstance(result_1, resourceclosing.FactoryService)
assert result_1.service.init_counter == 1
assert result_1.service.shutdown_counter == 1
result_2 = resourceclosing.test_function_dependency_kwargs()
assert isinstance(result_2, resourceclosing.FactoryService)
assert result_2.service.init_counter == 2
assert result_2.service.shutdown_counter == 2
@mark.usefixtures("resourceclosing_container")