Skip to content

Commit

Permalink
Add exclude_obj_callback_strict parameter for deepdiff
Browse files Browse the repository at this point in the history
  • Loading branch information
mskhviyu committed Jun 2, 2022
1 parent e3c2ba1 commit 0f6cf19
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def __init__(self,
cutoff_intersection_for_pairs=CUTOFF_INTERSECTION_FOR_PAIRS_DEFAULT,
encodings=None,
exclude_obj_callback=None,
exclude_obj_callback_strict=None,
exclude_paths=None,
exclude_regex_paths=None,
exclude_types=None,
Expand Down Expand Up @@ -194,6 +195,7 @@ def __init__(self,
self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group
self.ignore_string_case = ignore_string_case
self.exclude_obj_callback = exclude_obj_callback
self.exclude_obj_callback_strict = exclude_obj_callback_strict
self.number_to_string = number_to_string_func or number_to_string
self.iterable_compare_func = iterable_compare_func
self.ignore_private_variables = ignore_private_variables
Expand Down Expand Up @@ -429,6 +431,10 @@ def _skip_this(self, level):
elif self.exclude_obj_callback and \
(self.exclude_obj_callback(level.t1, level.path()) or self.exclude_obj_callback(level.t2, level.path())):
skip = True
elif self.exclude_obj_callback_strict and \
(self.exclude_obj_callback_strict(level.t1, level.path()) and
self.exclude_obj_callback_strict(level.t2, level.path())):
skip = True

return skip

Expand Down
12 changes: 12 additions & 0 deletions docs/ignore_types_or_values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ exclude_obj_callback: function, default = None
>>> DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback)
{}

exclude_obj_callback_strict: function, default = None
A function works the same way as exclude_obj_callback, but excludes elements from the result only if the function returns True for both elements

>>> def exclude_obj_callback_strict(obj, path):
... return True if isinstance(obj, int) and obj > 10 else False
...
>>> t1 = {"x": 10, "y": "b", "z": "c"}
>>> t2 = {"x": 12, "y": "b", "z": "c"}
>>> DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback_strict)
{}
>>> DeepDiff(t1, t2, exclude_obj_callback_strict=exclude_obj_callback_strict)
{'values_changed': {"root['x']": {'new_value': 12, 'old_value': 10}}}

.. _truncate_datetime_label:

Expand Down
10 changes: 10 additions & 0 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,16 @@ def exclude_obj_callback(obj, path):
result = {}
assert result == ddiff

def test_skip_exclude_obj_callback_strict(self):
def exclude_obj_callback_strict(obj, path):
return True if isinstance(obj, int) and obj > 10 else False

t1 = {"x": 10, "y": "b", "z": "c"}
t2 = {"x": 12, "y": "b", "z": "c"}
ddiff = DeepDiff(t1, t2, exclude_obj_callback_strict=exclude_obj_callback_strict)
result = {'values_changed': {"root['x']": {'new_value': 12, 'old_value': 10}}}
assert result == ddiff

def test_skip_str_type_in_dictionary(self):
t1 = {1: {2: "a"}}
t2 = {1: {}}
Expand Down

0 comments on commit 0f6cf19

Please sign in to comment.