Skip to content

Commit 0f6cf19

Browse files
committed
Add exclude_obj_callback_strict parameter for deepdiff
1 parent e3c2ba1 commit 0f6cf19

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

deepdiff/diff.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def __init__(self,
116116
cutoff_intersection_for_pairs=CUTOFF_INTERSECTION_FOR_PAIRS_DEFAULT,
117117
encodings=None,
118118
exclude_obj_callback=None,
119+
exclude_obj_callback_strict=None,
119120
exclude_paths=None,
120121
exclude_regex_paths=None,
121122
exclude_types=None,
@@ -194,6 +195,7 @@ def __init__(self,
194195
self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group
195196
self.ignore_string_case = ignore_string_case
196197
self.exclude_obj_callback = exclude_obj_callback
198+
self.exclude_obj_callback_strict = exclude_obj_callback_strict
197199
self.number_to_string = number_to_string_func or number_to_string
198200
self.iterable_compare_func = iterable_compare_func
199201
self.ignore_private_variables = ignore_private_variables
@@ -429,6 +431,10 @@ def _skip_this(self, level):
429431
elif self.exclude_obj_callback and \
430432
(self.exclude_obj_callback(level.t1, level.path()) or self.exclude_obj_callback(level.t2, level.path())):
431433
skip = True
434+
elif self.exclude_obj_callback_strict and \
435+
(self.exclude_obj_callback_strict(level.t1, level.path()) and
436+
self.exclude_obj_callback_strict(level.t2, level.path())):
437+
skip = True
432438

433439
return skip
434440

docs/ignore_types_or_values.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ exclude_obj_callback: function, default = None
272272
>>> DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback)
273273
{}
274274

275+
exclude_obj_callback_strict: function, default = None
276+
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
277+
278+
>>> def exclude_obj_callback_strict(obj, path):
279+
... return True if isinstance(obj, int) and obj > 10 else False
280+
...
281+
>>> t1 = {"x": 10, "y": "b", "z": "c"}
282+
>>> t2 = {"x": 12, "y": "b", "z": "c"}
283+
>>> DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback_strict)
284+
{}
285+
>>> DeepDiff(t1, t2, exclude_obj_callback_strict=exclude_obj_callback_strict)
286+
{'values_changed': {"root['x']": {'new_value': 12, 'old_value': 10}}}
275287

276288
.. _truncate_datetime_label:
277289

tests/test_diff_text.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,16 @@ def exclude_obj_callback(obj, path):
13941394
result = {}
13951395
assert result == ddiff
13961396

1397+
def test_skip_exclude_obj_callback_strict(self):
1398+
def exclude_obj_callback_strict(obj, path):
1399+
return True if isinstance(obj, int) and obj > 10 else False
1400+
1401+
t1 = {"x": 10, "y": "b", "z": "c"}
1402+
t2 = {"x": 12, "y": "b", "z": "c"}
1403+
ddiff = DeepDiff(t1, t2, exclude_obj_callback_strict=exclude_obj_callback_strict)
1404+
result = {'values_changed': {"root['x']": {'new_value': 12, 'old_value': 10}}}
1405+
assert result == ddiff
1406+
13971407
def test_skip_str_type_in_dictionary(self):
13981408
t1 = {1: {2: "a"}}
13991409
t2 = {1: {}}

0 commit comments

Comments
 (0)