forked from p15670423/monkey
Common: Define del_key() in terms of MutableMapping.pop()
This commit is contained in:
parent
e690eb7c61
commit
bc769ee6b8
|
@ -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 mapping: A mapping from which a key will be deleted
|
||||||
:param key: A key to delete from `mapping`
|
:param key: A key to delete from `mapping`
|
||||||
"""
|
"""
|
||||||
try:
|
mapping.pop(key, None)
|
||||||
del mapping[key]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
|
@ -35,8 +35,11 @@ def test_del_key__deletes_key():
|
||||||
def test_del_key__nonexistant_key():
|
def test_del_key__nonexistant_key():
|
||||||
key_to_delete = "a"
|
key_to_delete = "a"
|
||||||
my_dict = {"a": 1, "b": 2}
|
my_dict = {"a": 1, "b": 2}
|
||||||
|
assert key_to_delete in my_dict
|
||||||
|
|
||||||
del_key(my_dict, key_to_delete)
|
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
|
# This test passes if the following call does not raise an error
|
||||||
del_key(my_dict, key_to_delete)
|
del_key(my_dict, key_to_delete)
|
||||||
|
assert key_to_delete not in my_dict
|
||||||
|
|
Loading…
Reference in New Issue