Skip to content
Closed
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
5 changes: 3 additions & 2 deletions dictdiffer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ def nested_hash(obj):
if isinstance(obj, (list, tuple)):
return hash(tuple(map(nested_hash, obj)))
elif isinstance(obj, set):
return hash(tuple(map(nested_hash, sorted(obj))))
return hash(frozenset(obj))
elif isinstance(obj, dict):
return hash(tuple(map(nested_hash, sorted(obj.items()))))
return hash(frozenset((key, nested_hash(value))
for key, value in obj.items()))


def dot_lookup(source, lookup, parent=False):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,14 @@ def test_nested_hash(self):
nested_hash((1, 2, 3))
nested_hash(set([1, 2, 3]))
nested_hash({'foo': 'bar'})


def test_nested_hash_mixed_set(self):
first = {1, 'one', (2, 3)}
second = {(2, 3), 'one', 1}
self.assertEqual(nested_hash(first), nested_hash(second))

def test_nested_hash_mixed_dict_keys(self):
first = {1: ['one'], 'two': {3, 'three'}}
second = {'two': {'three', 3}, 1: ['one']}
self.assertEqual(nested_hash(first), nested_hash(second))