Common: Define del_key() in terms of MutableMapping.pop()

This commit is contained in:
Mike Salvatore 2022-09-11 22:56:50 -04:00
parent e690eb7c61
commit bc769ee6b8
2 changed files with 4 additions and 4 deletions

View File

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

View File

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