diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 2088484f..d112061a 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 afee7f7b..a5df21a8 100644 --- a/tests/unit/samples/wiringstringids/resourceclosing.py +++ b/tests/unit/samples/wiringstringids/resourceclosing.py @@ -41,6 +41,7 @@ class Container(containers.DeclarativeContainer): service = providers.Resource(init_service) factory_service = providers.Factory(FactoryService, service) + factory_service_kwargs = providers.Factory(FactoryService, service=service) nested_service = providers.Factory(NestedService, factory_service) @@ -55,6 +56,10 @@ def test_function_dependency(factory: FactoryService = Closing[Provide["factory_ @inject +def test_function_dependency_kwargs(factory: FactoryService = Closing[Provide["factory_service_kwargs"]]): + return factory + + def test_function_nested_dependency( nested: NestedService = Closing[Provide["nested_service"]] ): diff --git a/tests/unit/wiring/string_ids/test_main_py36.py b/tests/unit/wiring/string_ids/test_main_py36.py index 1507b8e7..6be84999 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")