diff --git a/deepdiff/diff.py b/deepdiff/diff.py index c044383a..7f75c0d1 100755 --- a/deepdiff/diff.py +++ b/deepdiff/diff.py @@ -2189,13 +2189,20 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree= if self._count_diff() is StopIteration: return + # When custom operators are set, check exclusions first so that + # operators never run on excluded paths (see #633). Without custom + # operators, the check below keeps the original order. + skip_this = self._skip_this(level) if self.custom_operators else None + if skip_this: + return + if self._use_custom_operator(level): return if level.t1 is level.t2: return - if self._skip_this(level): + if skip_this is None and self._skip_this(level): return report_type_change = True diff --git a/tests/test_operators.py b/tests/test_operators.py index 98444680..e2d95398 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -426,3 +426,36 @@ def normalize_value_for_hashing(self, parent: Any, obj: Any) -> Any: diff3 = DeepDiff(deepcopy(d1), deepcopy(d2), ignore_order=True, custom_operators=[RemoveGUIDsOperatorIgnoreOrderReady(types=[str])]) assert not diff3, "We shouldn't have a diff because we have normalized the string values to be all the same vlues." + def test_custom_operator_not_applied_to_excluded_path(self): + # https://github.com/qlustered/deepdiff/issues/633 + # Custom operators should not run on paths excluded via exclude_paths. + + class CustomClass: + def __init__(self, d: dict, lst: list): + self.dict = d + self.dict['list'] = lst + + visited = [] + + class ListMatchOperator(BaseOperator): + def give_up_diffing(self, level, diff_instance): + visited.append(level.path()) + if set(level.t1.dict['list']) == set(level.t2.dict['list']): + return True + + custom1 = CustomClass(d=dict(a=1, b=2), lst=[1, 2, 3]) + custom2 = CustomClass(d=dict(c=3, d=4), lst=[1, 2, 3, 2]) + custom3 = CustomClass(d=dict(a=1, b=2), lst=[1, 2, 3, 4]) + + t1 = {"a": custom1, "b": custom2} + t2 = {"a": custom1, "b": custom3} + + result = DeepDiff( + t1, + t2, + custom_operators=[ListMatchOperator(types=[CustomClass])], + exclude_paths=["root['b']"], + ) + assert result == {} + assert visited == ["root['a']"] +