Skip to content

Commit 2899d41

Browse files
committed
ignore case and ignore type subclass for deep hash
1 parent aa3a718 commit 2899d41

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

deepdiff/deephash.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from deepdiff.helper import (strings, numbers, unprocessed, not_hashed, add_to_frozen_set,
1212
convert_item_or_items_into_set_else_none, current_dir,
1313
convert_item_or_items_into_compiled_regexes_else_none,
14-
get_id)
14+
get_id, type_is_subclass_of_type_group, type_in_type_group)
1515
from deepdiff.base import Base
1616
logger = logging.getLogger(__name__)
1717

@@ -104,6 +104,7 @@ def __init__(self,
104104
# the only time it should be set to False is when
105105
# testing the individual hash functions for different types of objects.
106106
self.apply_hash = apply_hash
107+
self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group
107108

108109
self._hash(obj, parent="root", parents_ids=frozenset({get_id(obj)}))
109110

@@ -225,7 +226,7 @@ def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribut
225226
type_ = original_type or type(obj)
226227
type_str = type_.__name__
227228
for type_group in self.ignore_type_in_groups:
228-
if type_ in type_group:
229+
if self.type_check_func(type_, type_group):
229230
type_str = ','.join(map(lambda x: x.__name__, type_group))
230231
break
231232
else:

deepdiff/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,4 @@ def type_in_type_group(item, type_group):
202202

203203

204204
def type_is_subclass_of_type_group(item, type_group):
205-
return isinstance(item, type_group) or type_in_type_group(item, type_group)
205+
return isinstance(item, type_group) or issubclass(item, type_group) or type_in_type_group(item, type_group)

tests/test_hash.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,19 +335,38 @@ class Taco:
335335
def __init__(self):
336336
self.spicy = True
337337

338+
class ClassA:
339+
def __init__(self, x, y):
340+
self.x = x
341+
self.y = y
342+
343+
class ClassB:
344+
def __init__(self, x, y):
345+
self.x = x
346+
self.y = y
347+
348+
class ClassC(ClassB):
349+
pass
350+
351+
obj_a = ClassA(1, 2)
352+
obj_c = ClassC(1, 2)
353+
338354
burrito = Burrito()
339355
taco = Taco()
340356

341-
@pytest.mark.parametrize("t1, t2, ignore_type_in_groups, is_qual", [
342-
(taco, burrito, [], False),
343-
(taco, burrito, [(Taco, Burrito)], True),
344-
([taco], [burrito], [(Taco, Burrito)], True),
345-
357+
@pytest.mark.parametrize("t1, t2, ignore_type_in_groups, ignore_type_subclasses, is_qual", [
358+
# (taco, burrito, [], False, False),
359+
# (taco, burrito, [(Taco, Burrito)], False, True),
360+
# ([taco], [burrito], [(Taco, Burrito)], False, True),
361+
# ([obj_a], [obj_c], [(ClassA, ClassB)], False, False),
362+
([obj_a], [obj_c], [(ClassA, ClassB)], True, True),
346363
])
347-
def test_objects_with_same_content(self, t1, t2, ignore_type_in_groups, is_qual):
364+
def test_objects_with_same_content(self, t1, t2, ignore_type_in_groups, ignore_type_subclasses, is_qual):
348365

349-
t1_result = DeepHashPrep(t1, ignore_type_in_groups=ignore_type_in_groups)
350-
t2_result = DeepHashPrep(t2, ignore_type_in_groups=ignore_type_in_groups)
366+
t1_result = DeepHashPrep(t1, ignore_type_in_groups=ignore_type_in_groups,
367+
ignore_type_subclasses=ignore_type_subclasses)
368+
t2_result = DeepHashPrep(t2, ignore_type_in_groups=ignore_type_in_groups,
369+
ignore_type_subclasses=ignore_type_subclasses)
351370
assert is_qual == (t1_result[t1] == t2_result[t2])
352371

353372
def test_repetition_by_default_does_not_effect(self):
@@ -471,6 +490,15 @@ def test_skip_regex_path(self):
471490
assert 2 in t1_hash
472491
assert t1_hash[2] == t2_hash[2]
473492

493+
def test_string_case(self):
494+
t1 = "Hello"
495+
496+
t1_hash = DeepHashPrep(t1)
497+
assert t1_hash == {'Hello': 'str:Hello'}
498+
499+
t1_hash = DeepHashPrep(t1, ignore_string_case=True)
500+
assert t1_hash == {'Hello': 'str:hello'}
501+
474502

475503
class TestDeepHashSHA1:
476504
"""DeepHash with SHA1 Tests."""

0 commit comments

Comments
 (0)