From d8b37553e37f3b2768d0a74f5163fc1df92b3b33 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 5 Aug 2022 11:37:07 -0400 Subject: [PATCH] Common: Rename UnregisteredTypeError to UnresolvableDependencyError When calling `resolve()`, dependencies may be resolved as conventions, instances, or types. `UnregisteredTypeError` is overly specific and, in some cases, inaccurate. It makes more sense for `resolve()` to raise an `UnresolvableDependencyError` if it is unable to resolve a dependency. --- monkey/common/__init__.py | 2 +- monkey/common/di_container.py | 6 +++--- monkey/tests/common/stub_di_container.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) 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..b7b6b5e79 100644 --- a/monkey/common/di_container.py +++ b/monkey/common/di_container.py @@ -6,7 +6,7 @@ from common.utils.code_utils import del_key T = TypeVar("T") -class UnregisteredTypeError(ValueError): +class UnresolvableDependencyError(ValueError): pass @@ -109,7 +109,7 @@ class DIContainer: """ try: return self._resolve_type(type_) - except UnregisteredTypeError: + except UnresolvableDependencyError: pass args = self.resolve_dependencies(type_) @@ -153,7 +153,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()