Skip to content

Commit 66eb8f6

Browse files
committed
Merge branch 'gh-535/UUID-set-comparison-failure' of github.com:akshat62/deepdiff into gh-535/UUID-set-comparison-failure
2 parents 6b01e71 + b9af75d commit 66eb8f6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

deepdiff/deephash.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
import logging
33
import datetime
4+
import uuid
45
from typing import Union, Optional, Any, List, TYPE_CHECKING
56
from collections.abc import Iterable, MutableMapping
67
from collections import defaultdict
@@ -569,6 +570,17 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
569570
elif isinstance(obj, ipranges):
570571
result = self._prep_ipranges(obj)
571572

573+
elif isinstance(obj, uuid.UUID):
574+
# Handle UUID objects (including uuid6.UUID) by using their integer value
575+
result = f"uuid:{obj.int}"
576+
if self.apply_hash:
577+
result = self.hasher(result)
578+
try:
579+
self.hashes[obj] = (result, counts)
580+
except TypeError:
581+
self.hashes[get_id(obj)] = (result, counts)
582+
return result, counts
583+
572584
elif isinstance(obj, MutableMapping):
573585
result, counts = self._prep_dict(obj=obj, parent=parent, parents_ids=parents_ids)
574586

tests/test_hash.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ def test_numpy_datetime64(self):
208208
later_hash = DeepHash(later)
209209
assert a_hash[a] != later_hash[later]
210210

211+
def test_uuid6_hash_positive(self):
212+
"""Positive test case: Same UUID objects should produce the same hash."""
213+
import uuid6
214+
uuid_obj = uuid6.uuid7()
215+
hash1 = DeepHash(uuid_obj)
216+
hash2 = DeepHash(uuid_obj)
217+
assert hash1[uuid_obj] == hash2[uuid_obj]
218+
219+
import uuid
220+
regular_uuid = uuid.uuid4()
221+
hash_regular = DeepHash(regular_uuid)
222+
assert hash_regular[regular_uuid] is not unprocessed
223+
224+
def test_uuid6_deepdiff_negative(self):
225+
"""Negative test case: DeepDiff should detect differences between sets containing different UUID objects."""
226+
import uuid6
227+
dummy_id_1 = uuid6.uuid7()
228+
dummy_id_2 = uuid6.uuid7()
229+
set1 = {dummy_id_1}
230+
set2 = {dummy_id_1, dummy_id_2}
231+
diff = DeepDiff(set1, set2)
232+
assert diff != {}
233+
assert 'set_item_added' in diff
234+
assert str(dummy_id_2) in str(diff)
211235

212236
class TestDeepHashPrep:
213237
"""DeepHashPrep Tests covering object serialization."""

0 commit comments

Comments
 (0)