Common: Move DIContainer._del_key() to code_utils

This commit is contained in:
Mike Salvatore 2022-07-06 10:01:26 -04:00
parent e4edfd8ded
commit 3f3494e5d4
3 changed files with 46 additions and 22 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)