diff --git a/docs/api/immutabledict.rst b/docs/api/immutabledict.rst index 4e783c4..8bafcc5 100644 --- a/docs/api/immutabledict.rst +++ b/docs/api/immutabledict.rst @@ -1,5 +1,5 @@ immutabledict -====================== +============= .. autoclass:: immutabledict.immutabledict :exclude-members: items,keys,values diff --git a/immutabledict/__init__.py b/immutabledict/__init__.py index 6ea24fc..394a49b 100644 --- a/immutabledict/__init__.py +++ b/immutabledict/__init__.py @@ -142,6 +142,21 @@ def update(self, _dict: Dict[_K, _V]) -> immutabledict[_K, _V]: new.update(_dict) return self.__class__(new) + def discard(self, key: _K) -> immutabledict[_K, _V]: + """ + Return a new :class:`immutabledict` without the item at the given key. + + :param key: the key of the item you want to remove in the returned :class:`immutabledict` + + :return: the new :class:`immutabledict` without the item at the given + key, or a reference to itself if the key is not present + """ + # Based on the pyrsistent.PMap.discard() API + if key not in self: + return self + + return self.delete(key) + class ImmutableOrderedDict(immutabledict[_K, _V]): """ diff --git a/tests/test_immutabledict.py b/tests/test_immutabledict.py index a7636c2..0196946 100644 --- a/tests/test_immutabledict.py +++ b/tests/test_immutabledict.py @@ -235,6 +235,18 @@ def test_set_delete_update(self) -> None: # Make sure d doesn't change assert d == immutabledict(a=1, b=2) == dict(a=1, b=2) + def test_discard(self) -> None: + d: immutabledict[str, int] = immutabledict(a=1, b=2) + + # Key present + assert d.discard("a") == immutabledict(b=2) == {"b": 2} + assert hash(d.discard("a")) != hash(d) + + # Key not present + assert d.discard("c") == d == {"a": 1, "b": 2} + assert hash(d.discard("c")) == hash(d) + assert d.discard("c") is d + def test_new_kwargs(self) -> None: immutable_dict: immutabledict[str, int] = immutabledict(a=1, b=2) assert immutable_dict == {"a": 1, "b": 2} == dict(a=1, b=2)