Skip to content

Commit 975db98

Browse files
committed
Add exclude_obj_callback_strict parameter for deepdiff
1 parent 81341e2 commit 975db98

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

deepdiff/diff.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def _report_progress(_stats, progress_logger, duration):
9494
'ignore_type_subclasses',
9595
'ignore_string_case',
9696
'exclude_obj_callback',
97+
'exclude_obj_callback_strict',
9798
'ignore_private_variables',
9899
'encodings',
99100
'ignore_encoding_errors',
@@ -116,6 +117,7 @@ def __init__(self,
116117
cutoff_intersection_for_pairs=CUTOFF_INTERSECTION_FOR_PAIRS_DEFAULT,
117118
encodings=None,
118119
exclude_obj_callback=None,
120+
exclude_obj_callback_strict=None,
119121
exclude_paths=None,
120122
exclude_regex_paths=None,
121123
exclude_types=None,
@@ -194,6 +196,7 @@ def __init__(self,
194196
self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group
195197
self.ignore_string_case = ignore_string_case
196198
self.exclude_obj_callback = exclude_obj_callback
199+
self.exclude_obj_callback_strict = exclude_obj_callback_strict
197200
self.number_to_string = number_to_string_func or number_to_string
198201
self.iterable_compare_func = iterable_compare_func
199202
self.ignore_private_variables = ignore_private_variables
@@ -429,6 +432,10 @@ def _skip_this(self, level):
429432
elif self.exclude_obj_callback and \
430433
(self.exclude_obj_callback(level.t1, level.path()) or self.exclude_obj_callback(level.t2, level.path())):
431434
skip = True
435+
elif self.exclude_obj_callback_strict and \
436+
(self.exclude_obj_callback_strict(level.t1, level.path()) and
437+
self.exclude_obj_callback_strict(level.t2, level.path())):
438+
skip = True
432439

433440
return skip
434441

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)