Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

New __hash__ implementation up to documentation standards #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion flexdict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def __init__(self, data=None):
)

def __hash__(self):
return id(self)
import hashlib
locked_state = "l" if self.locked else "u"
hasher = hashlib.sha1()
for char in str(self) + locked_state:
hasher.update(char)
return hasher.hexdigest()

def __eq__(self, other):
if isinstance(other, dict):
Expand Down
11 changes: 11 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def test_equals():
assert (flex is FlexDict()) is False


def test_hash_equals():
"""Hash equality comparisons. hash(FlexDict()) == hash(FlexDict())"""
f = FlexDict()
g = FlexDict()
assert (hash(f) == hash(g)) is True
assert (hash(f) == hash(f)) is True
f["foo", "bar"] = 1
g["bar", "foo"] = 1
assert (hash(f) == hash(g)) is False


@mark.parametrize(
'keys', [
['a'],
Expand Down