diff --git a/monkey/common/__init__.py b/monkey/common/__init__.py index c10545c7f..fd9232730 100644 --- a/monkey/common/__init__.py +++ b/monkey/common/__init__.py @@ -1,5 +1,5 @@ """ Used for a common things between agent and island """ -from .di_container import DIContainer, UnregisteredTypeError +from .di_container import DIContainer, UnresolvableDependencyError from .operating_systems import OperatingSystems diff --git a/monkey/common/di_container.py b/monkey/common/di_container.py index 9220864c3..b07367962 100644 --- a/monkey/common/di_container.py +++ b/monkey/common/di_container.py @@ -1,4 +1,5 @@ import inspect +from contextlib import suppress from typing import Any, Sequence, Type, TypeVar from common.utils.code_utils import del_key @@ -6,7 +7,7 @@ from common.utils.code_utils import del_key T = TypeVar("T") -class UnregisteredTypeError(ValueError): +class UnresolvableDependencyError(ValueError): pass @@ -31,6 +32,7 @@ class DIContainer: :param interface: An interface or abstract base class that other classes depend upon :param concrete_type: A `type` (class) that implements `interface` + :raises TypeError: If `concrete_type` is not a class, or not a subclass of `interface` """ if not inspect.isclass(concrete_type): raise TypeError( @@ -54,6 +56,7 @@ class DIContainer: :param interface: An interface or abstract base class that other classes depend upon :param instance: An instance (object) of a `type` that implements `interface` + :raises TypeError: If `instance` is not an instance of `interface` """ if not isinstance(instance, interface): raise TypeError( @@ -106,11 +109,10 @@ class DIContainer: :param **type_**: A `type` (class) to construct :return: An instance of **type_** + :raises UnresolvableDependencyError: If any dependencies could not be successfully resolved """ - try: + with suppress(UnresolvableDependencyError): return self._resolve_type(type_) - except UnregisteredTypeError: - pass args = self.resolve_dependencies(type_) return type_(*args) @@ -125,6 +127,7 @@ class DIContainer: :param **type_**: A type (class) to resolve dependencies for :return: An Sequence of dependencies to be injected into `type_`'s constructor + :raises UnresolvableDependencyError: If any dependencies could not be successfully resolved """ args = [] @@ -153,7 +156,7 @@ class DIContainer: elif type_ in self._instance_registry: return self._retrieve_registered_instance(type_) - raise UnregisteredTypeError( + raise UnresolvableDependencyError( f'Failed to resolve unregistered type "{DIContainer._format_type_name(type)}"' ) diff --git a/monkey/tests/common/stub_di_container.py b/monkey/tests/common/stub_di_container.py index bed434145..5d9f6847f 100644 --- a/monkey/tests/common/stub_di_container.py +++ b/monkey/tests/common/stub_di_container.py @@ -1,7 +1,7 @@ from typing import Any, Sequence, Type, TypeVar from unittest.mock import MagicMock -from common import DIContainer, UnregisteredTypeError +from common import DIContainer, UnresolvableDependencyError T = TypeVar("T") @@ -10,11 +10,11 @@ class StubDIContainer(DIContainer): def resolve(self, type_: Type[T]) -> T: try: return super().resolve(type_) - except UnregisteredTypeError: + except UnresolvableDependencyError: return MagicMock() def resolve_dependencies(self, type_: Type[T]) -> Sequence[Any]: try: return super().resolve_dependencies(type_) - except UnregisteredTypeError: + except UnresolvableDependencyError: return MagicMock()