Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2135,13 +2135,17 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
if self._count_diff() is StopIteration:
return

if self._use_custom_operator(level):
# Custom operators may report differences even for identical objects.
if level.t1 is level.t2 and not self.custom_operators:
return

if level.t1 is level.t2:
if self._skip_this(level):
return

if self._skip_this(level):
if self._use_custom_operator(level):
return

if level.t1 is level.t2:
return

report_type_change = True
Expand Down
49 changes: 49 additions & 0 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,55 @@


class TestOperators:
@pytest.mark.parametrize('exclude_options', [
{'exclude_paths': ['b']},
{'exclude_paths': ["root['b']"]},
{'exclude_regex_paths': [r"\['b'\]$"]},
{'exclude_obj_callback': lambda obj, path: path == "root['b']"},
])
@pytest.mark.parametrize('prevent_default', [False, True])
def test_custom_operator_skips_excluded_paths(self, exclude_options, prevent_default):
matched_paths = []
compared_paths = []

class CustomClass:
def __init__(self, values):
self.values = values

class ListMatchOperator(BaseOperator):
def match(self, level):
matched_paths.append(level.path())
return super().match(level)

def give_up_diffing(self, level, diff_instance):
compared_paths.append(level.path())
if level.t1.values == level.t2.values:
diff_instance.custom_report_result('equal_lists', level, {})
return prevent_default

shared = CustomClass([1, 2, 3])
t1 = {'a': shared, 'b': CustomClass([1, 2, 3])}
t2 = {'a': shared, 'b': CustomClass([1, 2, 3, 4])}

result = DeepDiff(t1, t2, custom_operators=[ListMatchOperator(types=[CustomClass])], **exclude_options)

assert "root['b']" not in matched_paths
assert compared_paths == ["root['a']"]
assert result == {'equal_lists': {"root['a']": {}}}

@pytest.mark.parametrize('exclude_options', [
{'exclude_paths': ['root']},
{'exclude_types': [dict]},
])
def test_custom_operator_skips_excluded_root(self, exclude_options):
class UnexpectedOperator(BaseOperator):
def match(self, level):
raise AssertionError('Excluded objects should not reach custom operators')

result = DeepDiff({'a': 1}, {'a': 2}, custom_operators=[UnexpectedOperator()], **exclude_options)

assert result == {}

def test_custom_operators_prevent_default(self):
t1 = {
"coordinates": [
Expand Down