Skip to content

Commit

Permalink
fixing extra logging when key not found in the deep hashes #293
Browse files Browse the repository at this point in the history
  • Loading branch information
seperman committed Apr 10, 2022
1 parent 24af9ea commit b419c81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
16 changes: 10 additions & 6 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ def _create_hashtable(self, level, t):
apply_hash=True,
**self.deephash_parameters,
)
item_hash = deep_hash[item]
except UnicodeDecodeError as err:
err.reason = f"Can not produce a hash for {level.path()}: {err.reason}"
raise
Expand All @@ -796,12 +795,17 @@ def _create_hashtable(self, level, t):
"Not counting this object.\n %s" %
(level.path(), e))
else:
if item_hash is unprocessed: # pragma: no cover
logger.warning("Item %s was not processed while hashing "
"thus not counting this object." %
level.path())
try:
item_hash = deep_hash[item]
except KeyError:
pass
else:
self._add_hash(hashes=local_hashes, item_hash=item_hash, item=item, i=i)
if item_hash is unprocessed: # pragma: no cover
logger.warning("Item %s was not processed while hashing "
"thus not counting this object." %
level.path())
else:
self._add_hash(hashes=local_hashes, item_hash=item_hash, item=item, i=i)

# Also we hash the iterables themselves too so that we can later create cache keys from those hashes.
try:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_ignore_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import re
from unittest import mock
from deepdiff.helper import number_to_string, CannotCompare
from deepdiff import DeepDiff
Expand Down Expand Up @@ -1014,6 +1015,9 @@ def ignore_order_func(level):
}
assert expected == ddiff


class TestDecodingErrorIgnoreOrder:

EXPECTED_MESSAGE1 = (
"'utf-8' codec can't decode byte 0xc3 in position 0: Can not produce a hash for root: invalid continuation byte in '('. "
"Please either pass ignore_encoding_errors=True or pass the encoding via encodings=['utf-8', '...'].")
Expand All @@ -1039,3 +1043,18 @@ def test_diff_encodings(self, mock_logger, test_num, item, encodings, ignore_enc
else:
result = DeepDiff([b'foo'], [item], encodings=encodings, ignore_encoding_errors=ignore_encoding_errors, ignore_order=True)
assert expected_result == result, f"test_diff_encodings test #{test_num} failed."


class TestErrorMessagesWhenIgnoreOrder:

@mock.patch('deepdiff.diff.logger')
def test_error_messages_when_ignore_order(self, mock_logger):
t1 = {'x': 0, 'y': [0, 'a', 'b', 'c']}
t2 = {'x': 1, 'y': [1, 'c', 'b', 'a']}

exclude = [re.compile(r"\['x'\]"), re.compile(r"\['y'\]\[0\]")]

result = DeepDiff(t1, t2, ignore_order=True, exclude_regex_paths=exclude)
assert {} == result

assert not mock_logger.error.called

0 comments on commit b419c81

Please sign in to comment.