Skip to content
Snippets Groups Projects
Commit 06837d5c authored by vlorentz's avatar vlorentz
Browse files

Implement ImmutableDict.__hash__.

parent c4dad17f
No related branches found
No related tags found
No related merge requests found
......@@ -42,6 +42,9 @@ class ImmutableDict(Mapping, Generic[KT, VT]):
def items(self):
yield from self.data
def __hash__(self):
return hash(tuple(sorted(self.data)))
def copy_pop(self, popped_key) -> Tuple[Optional[VT], "ImmutableDict[KT, VT]"]:
"""Returns a copy of this ImmutableDict without the given key,
as well as the value associated to the key."""
......
......@@ -64,3 +64,23 @@ def test_immutabledict_copy_pop():
assert d.copy_pop("foo") == ("bar", ImmutableDict({"baz": "qux"}))
assert d.copy_pop("not a key") == (None, d)
def test_hash():
assert hash(ImmutableDict()) == hash(ImmutableDict({}))
assert hash(ImmutableDict({"foo": "bar"})) == hash(ImmutableDict({"foo": "bar"}))
assert hash(ImmutableDict({"foo": "bar", "baz": "qux"})) == hash(
ImmutableDict({"foo": "bar", "baz": "qux"})
)
assert hash(ImmutableDict({"foo": "bar", "baz": "qux"})) == hash(
ImmutableDict({"baz": "qux", "foo": "bar"})
)
def test_equality_order():
assert ImmutableDict({"foo": "bar", "baz": "qux"}) == ImmutableDict(
{"foo": "bar", "baz": "qux"}
)
assert ImmutableDict({"foo": "bar", "baz": "qux"}) == ImmutableDict(
{"baz": "qux", "foo": "bar"}
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment