From c53864cdd72df890f8770f2a688cbe3071800e01 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 16 Jun 2022 09:12:09 -0400 Subject: [PATCH] Common: Add release_convention() --- monkey/common/di_container.py | 10 ++++++++++ monkey/tests/unit_tests/common/test_di_container.py | 9 +++++++++ vulture_allowlist.py | 1 + 3 files changed, 20 insertions(+) diff --git a/monkey/common/di_container.py b/monkey/common/di_container.py index a6eccddad..71d337ab3 100644 --- a/monkey/common/di_container.py +++ b/monkey/common/di_container.py @@ -171,6 +171,16 @@ class DIContainer: DIContainer._del_key(self._type_registry, interface) DIContainer._del_key(self._instance_registry, interface) + def release_convention(self, type_: Type[T], name: str): + """ + Deregister a convention + + :param **type_**: The `type` (class) of the dependency + :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): """ diff --git a/monkey/tests/unit_tests/common/test_di_container.py b/monkey/tests/unit_tests/common/test_di_container.py index 81e0220b4..20f492e43 100644 --- a/monkey/tests/unit_tests/common/test_di_container.py +++ b/monkey/tests/unit_tests/common/test_di_container.py @@ -366,3 +366,12 @@ def test_register_convention__instance_properly_resolved(container): assert id(service_a_instance) == id(test_9.service_a) assert test_9.my_str == my_str + + +def test_release_convention(container): + my_str = "test_string" + container.register_convention(str, "my_str", my_str) + + with pytest.raises(ValueError): + container.release_convention(str, "my_str") + container.resolve(TestClass6) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index ab09127b3..d451f367d 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -182,6 +182,7 @@ GCPHandler # unused function (envs/monkey_zoo/blackbox/test_blackbox.py:57) architecture # unused variable (monkey/infection_monkey/exploit/caching_agent_repository.py:25) response_code # unused variable (monkey/monkey_island/cc/services/aws/aws_command_runner.py:26) +release_convention # unused method (monkey/common/di_container.py:174) # TODO DELETE AFTER RESOURCE REFACTORING NetworkMap