From bc769ee6b842ba6a5866ecc6acc27ced060e9c2d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Sun, 11 Sep 2022 22:56:50 -0400 Subject: [PATCH] Common: Define del_key() in terms of MutableMapping.pop() --- monkey/common/utils/code_utils.py | 5 +---- monkey/tests/unit_tests/common/utils/test_code_utils.py | 3 +++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/monkey/common/utils/code_utils.py b/monkey/common/utils/code_utils.py index 21c0ce175..4da2f4b87 100644 --- a/monkey/common/utils/code_utils.py +++ b/monkey/common/utils/code_utils.py @@ -44,7 +44,4 @@ def del_key(mapping: MutableMapping[T, Any], key: T): :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 + mapping.pop(key, None) 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 e5980723d..6b0e84214 100644 --- a/monkey/tests/unit_tests/common/utils/test_code_utils.py +++ b/monkey/tests/unit_tests/common/utils/test_code_utils.py @@ -35,8 +35,11 @@ def test_del_key__deletes_key(): def test_del_key__nonexistant_key(): key_to_delete = "a" my_dict = {"a": 1, "b": 2} + assert key_to_delete in my_dict del_key(my_dict, key_to_delete) + assert key_to_delete not in my_dict # This test passes if the following call does not raise an error del_key(my_dict, key_to_delete) + assert key_to_delete not in my_dict