Add issubclass check for init_resources

This commit is contained in:
ZipFile 2025-06-16 08:12:06 +00:00
parent c6c1797075
commit d11b5c10b7
2 changed files with 21 additions and 0 deletions

View File

@ -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:

View File

@ -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)