Skip to content

Commit 95c9f83

Browse files
committed
adding test case for hashing class. Ignore private keys when calculating hash.
1 parent 6623290 commit 95c9f83

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

AUTHORS

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
Authors:
1+
Many thanks to the following people for their contributions to DeepDiff:
2+
23
- Seperman
34
- Victor Hahn Castell @ Flexoptix
45
- nfvs for Travis-CI setup script.
56
- brbsix for initial Py3 porting.
6-
- WangFenjin for unicode support.
7+
- WangFenjin for Unicode support.
78
- timoilya for comparing list of sets when ignoring order.
89
- Bernhard10 for significant digits comparison.
910
- b-jazz for PEP257 cleanup, Standardize on full names, fixing line endings.
@@ -21,3 +22,5 @@ Authors:
2122
- Juan Soler (Soleronline) for adding ignore_type_number
2223
- mthaddon for adding timedelta diffing support
2324
- Necrophagos for Hashing of the number 1 vs. True
25+
- Hugo (hugovk) for fixes for Python 3.10 and dropping support for EOL Python 3.4
26+
- Andrey Gavrilin (gaal-dev) for hashing classes.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ And then running
417417

418418
# ChangeLog
419419

420+
- v4-2-0: Fix for Py3.10. Dropping support for EOL Python 3.4. Ignoring private keys when calculating hashes. For example __init__ is not a part of hash calculation anymore.
420421
- v4-1-0: .json property is finally removed.
421422
- v4-0-9: Fixing the bug for hashing custom unhashable objects
422423
- v4-0-8: Adding ignore_nan_inequality for float('nan')

deepdiff/deephash.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def __contains__(self, obj):
170170
return super().__contains__(key)
171171

172172
def _prep_obj(self, obj, parent, parents_ids=EMPTY_FROZENSET, is_namedtuple=False):
173-
"""Difference of 2 objects"""
173+
"""prepping objects"""
174174
original_type = type(obj) if not isinstance(obj, type) else obj
175175
try:
176176
if is_namedtuple:
@@ -208,6 +208,9 @@ def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribut
208208

209209
key_text = "%s{}".format(INDEX_VS_ATTRIBUTE[print_as_attribute])
210210
for key, item in obj.items():
211+
# ignore private variables
212+
if isinstance(key, str) and key.startswith('__'):
213+
continue
211214
key_formatted = "'%s'" % key if not print_as_attribute and isinstance(key, strings) else key
212215
key_in_report = key_text % (parent, key_formatted)
213216

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ Indices and tables
281281
Changelog
282282
=========
283283

284+
- v4-2-0: Fix for Py3.10. Dropping support for EOL Python 3.4. Ignoring private keys when calculating hashes. For example __init__ is not a part of hash calculation anymore.
285+
- v4-1-0: .json property is finally removed.
284286
- v4-0-9: Fixing the bug for hashing custom unhashable objects
285287
- v4-0-8: Adding ignore_nan_inequality for float('nan')
286288
- v4-0-7: Hashing of the number 1 vs. True

tests/test_hash.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
logging.disable(logging.CRITICAL)
1313

1414

15-
class CustomClass:
15+
class ClassC:
16+
class_attr = 0
17+
1618
def __init__(self, a, b=None):
1719
self.a = a
1820
self.b = b
1921

2022
def __str__(self):
2123
return "({}, {})".format(self.a, self.b)
2224

23-
def __repr__(self):
24-
return self.__str__()
25+
__repr__ = __str__
2526

2627

2728
# Only the prep part of DeepHash. We don't need to test the actual hash function.
@@ -537,6 +538,14 @@ def test_string_case(self):
537538
t1_hash = DeepHashPrep(t1, ignore_string_case=True)
538539
assert t1_hash == {'Hello': 'str:hello'}
539540

541+
def test_hash_class(self):
542+
t1 = ClassC
543+
t1_hash = DeepHashPrep(t1)
544+
assert t1_hash['class_attr'] == 'str:class_attr'
545+
assert t1_hash[0] == 'int:0'
546+
# Note: we ignore private names in calculating hashes now. So you dont see __init__ here for example.
547+
assert t1_hash[t1] == r'objClassC:{str:class_attr:int:0}'
548+
540549

541550
class TestDeepHashSHA:
542551
"""DeepHash with SHA Tests."""

0 commit comments

Comments
 (0)