From 3f3494e5d401d950e5936dd6f566eb038b0dff6e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 6 Jul 2022 10:01:26 -0400 Subject: [PATCH] Common: Move DIContainer._del_key() to code_utils --- monkey/common/di_container.py | 28 ++++++------------- monkey/common/utils/code_utils.py | 18 +++++++++++- .../common/utils/test_code_utils.py | 22 ++++++++++++++- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/monkey/common/di_container.py b/monkey/common/di_container.py index 0a9748f4b..c1c740e39 100644 --- a/monkey/common/di_container.py +++ b/monkey/common/di_container.py @@ -1,5 +1,7 @@ import inspect -from typing import Any, MutableMapping, Sequence, Type, TypeVar +from typing import Any, Sequence, Type, TypeVar + +from common.utils.code_utils import del_key T = TypeVar("T") @@ -43,7 +45,7 @@ class DIContainer: ) self._type_registry[interface] = concrete_type - DIContainer._del_key(self._instance_registry, interface) + del_key(self._instance_registry, interface) def register_instance(self, interface: Type[T], instance: T): """ @@ -59,7 +61,7 @@ class DIContainer: ) self._instance_registry[interface] = instance - DIContainer._del_key(self._type_registry, interface) + del_key(self._type_registry, interface) def register_convention(self, type_: Type[T], name: str, instance: T): """ @@ -168,8 +170,8 @@ class DIContainer: :param interface: The interface to release """ - DIContainer._del_key(self._type_registry, interface) - DIContainer._del_key(self._instance_registry, interface) + del_key(self._type_registry, interface) + del_key(self._instance_registry, interface) def release_convention(self, type_: Type[T], name: str): """ @@ -179,18 +181,4 @@ class DIContainer: :param name: The name of the dependency parameter """ convention_identifier = (type_, name) - DIContainer._del_key(self._convention_registry, convention_identifier) - - @staticmethod - def _del_key(mapping: MutableMapping[T, Any], key: T): - """ - Deletes key from mapping. Unlike the `del` keyword, this function does not raise a KeyError - if the key does not exist. - - :param mapping: A mapping from which a key will be deleted - :param key: A key to delete from `mapping` - """ - try: - del mapping[key] - except KeyError: - pass + del_key(self._convention_registry, convention_identifier) diff --git a/monkey/common/utils/code_utils.py b/monkey/common/utils/code_utils.py index 251ce9375..a0fe6b5a9 100644 --- a/monkey/common/utils/code_utils.py +++ b/monkey/common/utils/code_utils.py @@ -1,5 +1,7 @@ import queue -from typing import Any, List +from typing import Any, List, MutableMapping, TypeVar + +T = TypeVar("T") class abstractstatic(staticmethod): @@ -30,3 +32,17 @@ def queue_to_list(q: queue.Queue) -> List[Any]: pass return list_ + + +def del_key(mapping: MutableMapping[T, Any], key: T): + """ + Deletes key from mapping. Unlike the `del` keyword, this function does not raise a KeyError + if the key does not exist. + + :param mapping: A mapping from which a key will be deleted + :param key: A key to delete from `mapping` + """ + try: + del mapping[key] + except KeyError: + pass diff --git a/monkey/tests/unit_tests/common/utils/test_code_utils.py b/monkey/tests/unit_tests/common/utils/test_code_utils.py index 411b07a63..e5980723d 100644 --- a/monkey/tests/unit_tests/common/utils/test_code_utils.py +++ b/monkey/tests/unit_tests/common/utils/test_code_utils.py @@ -1,6 +1,6 @@ from queue import Queue -from common.utils.code_utils import queue_to_list +from common.utils.code_utils import del_key, queue_to_list def test_empty_queue_to_empty_list(): @@ -20,3 +20,23 @@ def test_queue_to_list(): list_ = queue_to_list(q) assert list_ == expected_list + + +def test_del_key__deletes_key(): + key_to_delete = "a" + my_dict = {"a": 1, "b": 2} + expected_dict = {k: v for k, v in my_dict.items() if k != key_to_delete} + + del_key(my_dict, key_to_delete) + + assert my_dict == expected_dict + + +def test_del_key__nonexistant_key(): + key_to_delete = "a" + my_dict = {"a": 1, "b": 2} + + del_key(my_dict, key_to_delete) + + # This test passes if the following call does not raise an error + del_key(my_dict, key_to_delete)