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)