diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index b1f01622..f62b4f31 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -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 diff --git a/tests/unit/samples/wiringstringids/resourceclosing.py b/tests/unit/samples/wiringstringids/resourceclosing.py index 6360e15c..a143ad1d 100644 --- a/tests/unit/samples/wiringstringids/resourceclosing.py +++ b/tests/unit/samples/wiringstringids/resourceclosing.py @@ -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 diff --git a/tests/unit/wiring/string_ids/test_main_py36.py b/tests/unit/wiring/string_ids/test_main_py36.py index d4c49fe8..826ef80c 100644 --- a/tests/unit/wiring/string_ids/test_main_py36.py +++ b/tests/unit/wiring/string_ids/test_main_py36.py @@ -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")