From d11b5c10b7cd8f11d08b4741e5b34587f14053be Mon Sep 17 00:00:00 2001 From: ZipFile Date: Mon, 16 Jun 2025 08:12:06 +0000 Subject: [PATCH] Add issubclass check for init_resources --- src/dependency_injector/containers.pyx | 8 ++++++++ tests/unit/containers/instance/test_main_py2_py3.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/dependency_injector/containers.pyx b/src/dependency_injector/containers.pyx index 818fd61a..99762da2 100644 --- a/src/dependency_injector/containers.pyx +++ b/src/dependency_injector/containers.pyx @@ -317,6 +317,10 @@ class DynamicContainer(Container): def init_resources(self, resource_type=providers.Resource): """Initialize all container resources.""" + + if not issubclass(resource_type, providers.Resource): + raise TypeError("resource_type must be a subclass of Resource provider") + futures = [] for provider in self.traverse(types=[resource_type]): @@ -330,6 +334,10 @@ class DynamicContainer(Container): def shutdown_resources(self, resource_type=providers.Resource): """Shutdown all container resources.""" + + if not issubclass(resource_type, providers.Resource): + raise TypeError("resource_type must be a subclass of Resource provider") + def _independent_resources(resources): for resource in resources: for other_resource in resources: diff --git a/tests/unit/containers/instance/test_main_py2_py3.py b/tests/unit/containers/instance/test_main_py2_py3.py index ddd61de9..0c19fa36 100644 --- a/tests/unit/containers/instance/test_main_py2_py3.py +++ b/tests/unit/containers/instance/test_main_py2_py3.py @@ -325,6 +325,19 @@ def test_init_shutdown_nested_resources(): assert _init2.shutdown_counter == 2 +def test_init_shutdown_resources_wrong_type() -> None: + class Container(containers.DeclarativeContainer): + pass + + c = Container() + + with raises(TypeError, match=r"resource_type must be a subclass of Resource provider"): + c.init_resources(int) # type: ignore[arg-type] + + with raises(TypeError, match=r"resource_type must be a subclass of Resource provider"): + c.shutdown_resources(int) # type: ignore[arg-type] + + def test_reset_singletons(): class SubSubContainer(containers.DeclarativeContainer): singleton = providers.Singleton(object)