Skip to content

Commit 5e0ca40

Browse files
committed
Add issubclass check for init_resources
1 parent 15c0b00 commit 5e0ca40

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/dependency_injector/containers.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ class DynamicContainer(Container):
335335

336336
def init_resources(self, resource_type=providers.Resource):
337337
"""Initialize all container resources."""
338+
339+
if not issubclass(resource_type, providers.Resource):
340+
raise ValueError("resource_type must be a subclass of Resource provider")
341+
338342
futures = []
339343

340344
for provider in self.traverse(types=[resource_type]):
@@ -348,6 +352,10 @@ class DynamicContainer(Container):
348352

349353
def shutdown_resources(self, resource_type=providers.Resource):
350354
"""Shutdown all container resources."""
355+
356+
if not issubclass(resource_type, providers.Resource):
357+
raise ValueError("resource_type must be a subclass of Resource provider")
358+
351359
def _independent_resources(resources):
352360
for resource in resources:
353361
for other_resource in resources:

tests/unit/containers/instance/test_main_py2_py3.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,19 @@ class Container(containers.DeclarativeContainer):
325325
assert _init2.shutdown_counter == 2
326326

327327

328+
def test_init_shutdown_resources_wrong_type() -> None:
329+
class Container(containers.DeclarativeContainer):
330+
pass
331+
332+
c = Container()
333+
334+
with raises(ValueError, match=r"resource_type must be a subclass of Resource provider"):
335+
c.init_resources(int) # type: ignore[arg-type]
336+
337+
with raises(ValueError, match=r"resource_type must be a subclass of Resource provider"):
338+
c.shutdown_resources(int) # type: ignore[arg-type]
339+
340+
328341
def test_reset_singletons():
329342
class SubSubContainer(containers.DeclarativeContainer):
330343
singleton = providers.Singleton(object)

0 commit comments

Comments
 (0)