mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-01 10:53:14 +03:00
Add issubclass check for init_resources
This commit is contained in:
parent
c6c1797075
commit
d11b5c10b7
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user