Skip to content

Commit b769cae

Browse files
author
Bobby Morck
committed
Add Ignore List Order Option to DeepHash
1 parent 8951d92 commit b769cae

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

deepdiff/deephash.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def __init__(self,
144144
parent="root",
145145
encodings=None,
146146
ignore_encoding_errors=False,
147+
ignore_list_order=True,
147148
**kwargs):
148149
if kwargs:
149150
raise ValueError(
@@ -190,6 +191,7 @@ def __init__(self,
190191
self.ignore_private_variables = ignore_private_variables
191192
self.encodings = encodings
192193
self.ignore_encoding_errors = ignore_encoding_errors
194+
self.ignore_list_order = ignore_list_order
193195

194196
self._hash(obj, parent=parent, parents_ids=frozenset({get_id(obj)}))
195197

@@ -424,7 +426,9 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET):
424426
'{}|{}'.format(i, v) for i, v in result.items()
425427
]
426428

427-
result = sorted(map(str, result)) # making sure the result items are string and sorted so join command works.
429+
result = map(str, result) # making sure the result items are string so join command works.
430+
if self.ignore_list_order:
431+
result = sorted(result)
428432
result = ','.join(result)
429433
result = KEY_TO_VAL_STR.format(type(obj).__name__, result)
430434

tests/test_hash.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,21 @@ def test_same_sets_same_hash(self):
368368
t2_hash = DeepHashPrep(t2)
369369

370370
assert t1_hash[get_id(t1)] == t2_hash[get_id(t2)]
371+
372+
@pytest.mark.parametrize("list1, list2, ignore_list_order, is_equal", [
373+
([1, 2], [2, 1], False, False),
374+
([1, 2], [2, 1], True, True),
375+
([1, 2, 3], [1, 3, 2], False, False),
376+
([1, [1, 2, 3]], [1, [3, 2, 1]], False, False),
377+
([1, [1, 2, 3]], [1, [3, 2, 1]], True, True),
378+
((1, 2), (2, 1), False, False),
379+
((1, 2), (2, 1), True, True),
380+
])
381+
def test_list_ignore_order(self, list1, list2, ignore_list_order, is_equal):
382+
list1_hash = DeepHash(list1, ignore_list_order=ignore_list_order)
383+
list2_hash = DeepHash(list2, ignore_list_order=ignore_list_order)
384+
385+
assert is_equal == (list1_hash[list1] == list2_hash[list2])
371386

372387
@pytest.mark.parametrize("t1, t2, significant_digits, number_format_notation, result", [
373388
({0.012, 0.98}, {0.013, 0.99}, 1, "f", 'set:float:0.0,float:1.0'),

0 commit comments

Comments
 (0)