Skip to content

Commit

Permalink
reverted calculation of hash using frozenset
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco-Sulla committed Jan 27, 2020
1 parent 698abd8 commit 10303a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion frozendict/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __hash__(self, *args, **kwargs):
try:
# try to cache the hash. You have to use `object.__setattr__()`
# because the `__setattr__` of the class is inhibited
_hash = hash(tuple(self.items()))
_hash = hash(frozenset(self.items()))
except Exception:
# object is not hashable
object.__setattr__(self, "_hash", "unhashable")
Expand Down
11 changes: 10 additions & 1 deletion test/test_frozendict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
def fd_dict():
return {"Sulla": "Marco", "Hicks": "Bill", frozendict({1: 2}): "frozen"}

@pytest.fixture
def fd_dict_eq():
return {"Hicks": "Bill", "Sulla": "Marco", frozendict({1: 2}): "frozen"}

@pytest.fixture
def fd_dict_2():
return {"Sulla": "Marco", "Hicks": "Bill", "frozen": frozendict({1: 2})}
Expand All @@ -14,6 +18,10 @@ def fd_dict_2():
def fd(fd_dict):
return frozendict(fd_dict)

@pytest.fixture
def fd_eq(fd_dict_eq):
return frozendict(fd_dict_eq)

@pytest.fixture
def fd2(fd_dict_2):
return frozendict(fd_dict_2)
Expand Down Expand Up @@ -83,8 +91,9 @@ def test_not_equal(fd, fd_giulia):
def test_equals_dict(fd, fd_dict):
assert fd == fd_dict

def test_hash(fd):
def test_hash(fd, fd_eq):
assert hash(fd)
assert hash(fd) == hash(fd_eq)

def test_pickle(fd):
fd_unpickled = pickle.loads(pickle.dumps(fd))
Expand Down

0 comments on commit 10303a8

Please sign in to comment.