Skip to content

Commit

Permalink
text view only show new_path for iterable item moved OR if
Browse files Browse the repository at this point in the history
verbose_level=2 and the new_path is different than path
  • Loading branch information
seperman committed Apr 8, 2024
1 parent 3d3bfd8 commit d76e2e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
24 changes: 18 additions & 6 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,9 @@ def _diff_dict(
notpresent,
t2[key],
child_relationship_class=rel_class,
child_relationship_param=key)
child_relationship_param=key,
child_relationship_param2=key,
)
self._report_result(item_added_key, change_level, local_tree=local_tree)

for key in t_keys_removed:
Expand All @@ -599,7 +601,9 @@ def _diff_dict(
t1[key],
notpresent,
child_relationship_class=rel_class,
child_relationship_param=key)
child_relationship_param=key,
child_relationship_param2=key,
)
self._report_result(item_removed_key, change_level, local_tree=local_tree)

for key in t_keys_intersect: # key present in both dicts - need to compare values
Expand All @@ -618,7 +622,9 @@ def _diff_dict(
t1[key1],
t2[key2],
child_relationship_class=rel_class,
child_relationship_param=key)
child_relationship_param=key,
child_relationship_param2=key,
)
self._diff(next_level, parents_ids_added, local_tree=local_tree)

def _diff_set(self, level, local_tree=None):
Expand Down Expand Up @@ -943,15 +949,19 @@ def _diff_ordered_iterable_by_difflib(
x,
notpresent,
child_relationship_class=child_relationship_class,
child_relationship_param=index + t1_from_index)
child_relationship_param=index + t1_from_index,
child_relationship_param2=index + t1_from_index,
)
self._report_result('iterable_item_removed', change_level, local_tree=local_tree)
elif tag == 'insert':
for index, y in enumerate(level.t2[t2_from_index:t2_to_index]):
change_level = level.branch_deeper(
notpresent,
y,
child_relationship_class=child_relationship_class,
child_relationship_param=index + t2_from_index)
child_relationship_param=index + t2_from_index,
child_relationship_param2=index + t2_from_index,
)
self._report_result('iterable_item_added', change_level, local_tree=local_tree)
return opcodes_with_values

Expand Down Expand Up @@ -1501,7 +1511,9 @@ def _diff_numpy_array(self, level, parents_ids=frozenset(), local_tree=None):
t1_row,
t2_row,
child_relationship_class=NumpyArrayRelationship,
child_relationship_param=t1_path)
child_relationship_param=t1_path,
child_relationship_param2=t2_path,
)

self._diff_iterable_in_order(new_level, parents_ids, _original_type=_original_type, local_tree=local_tree)

Expand Down
18 changes: 10 additions & 8 deletions deepdiff/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ def _from_tree_type_changes(self, tree):
if 'type_changes' in tree:
for change in tree['type_changes']:
path = change.path(force=FORCE_DEFAULT)
new_path = change.path(use_t2=True, force=FORCE_DEFAULT)
if type(change.t1) is type:
include_values = False
old_type = change.t1
Expand All @@ -202,8 +201,10 @@ def _from_tree_type_changes(self, tree):
'old_type': old_type,
'new_type': new_type,
})
if path != new_path:
remap_dict['new_path'] = new_path
if self.verbose_level > 1:
new_path = change.path(use_t2=True, force=FORCE_DEFAULT)
if path != new_path:
remap_dict['new_path'] = new_path
self['type_changes'][path] = remap_dict
if self.verbose_level and include_values:
remap_dict.update(old_value=change.t1, new_value=change.t2)
Expand All @@ -212,10 +213,11 @@ def _from_tree_value_changed(self, tree):
if 'values_changed' in tree and self.verbose_level > 0:
for change in tree['values_changed']:
path = change.path(force=FORCE_DEFAULT)
new_path = change.path(use_t2=True, force=FORCE_DEFAULT)
the_changed = {'new_value': change.t2, 'old_value': change.t1}
if path != new_path:
the_changed['new_path'] = new_path
if self.verbose_level > 1:
new_path = change.path(use_t2=True, force=FORCE_DEFAULT)
if path != new_path:
the_changed['new_path'] = new_path
self['values_changed'][path] = the_changed
if 'diff' in change.additional:
the_changed.update({'diff': change.additional['diff']})
Expand Down Expand Up @@ -717,8 +719,8 @@ def path(self, root="root", force=None, get_parent_too=False, use_t2=False, outp
# traverse all levels of this relationship
while level and level is not self:
# get this level's relationship object
if(use_t2):
next_rel = level.t2_child_rel
if use_t2:
next_rel = level.t2_child_rel or level.t1_child_rel
else:
next_rel = level.t1_child_rel or level.t2_child_rel # next relationship object to get a formatted param from

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def test_cache_deeply_nested_b(self, nested_b_t1, nested_b_t2, nested_b_result):

stats = diff.get_stats()
expected_stats = {
'PASSES COUNT': 110,
'DIFF COUNT': 306,
'PASSES COUNT': 104,
'DIFF COUNT': 288,
'DISTANCE CACHE HIT COUNT': 0,
'MAX PASS LIMIT REACHED': False,
'MAX DIFF LIMIT REACHED': False
Expand Down
2 changes: 1 addition & 1 deletion tests/test_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_get_distance_works_event_when_ignore_order_and_different_hasher(self):
diff = DeepDiff(t1, t2, ignore_order=True, get_deep_distance=True,
cache_size=100, hasher=sha256hex)
dist = diff['deep_distance']
assert str(dist)[:4] == '0.44'
assert str(dist)[:4] == '0.55'

def test_get_distance_does_not_care_about_the_size_of_string(self):
t1 = ["a", "b"]
Expand Down
12 changes: 6 additions & 6 deletions tests/test_ignore_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,8 @@ def test_bool_vs_number(self):

@pytest.mark.parametrize('max_passes, expected', [
(0, {'values_changed': {'root[0]': {'new_value': {'key5': 'CHANGE', 'key6': 'val6'}, 'old_value': {'key3': [[[[[1, 2, 4, 5]]]]], 'key4': [7, 8]}}, 'root[1]': {'new_value': {'key3': [[[[[1, 3, 5, 4]]]]], 'key4': [7, 8]}, 'old_value': {'key5': 'val5', 'key6': 'val6'}}}}),
(1, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5'}, "root[0]['key3'][0]": {'new_value': [[[[1, 3, 5, 4]]]], 'old_value': [[[[1, 2, 4, 5]]]]}}}),
(22, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5'}, "root[0]['key3'][0][0][0][0][1]": {'new_value': 3, 'old_value': 2}}})
(1, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}, "root[0]['key3'][0]": {'new_value': [[[[1, 3, 5, 4]]]], 'old_value': [[[[1, 2, 4, 5]]]], 'new_path': "root[1]['key3'][0]"}}}),
(22, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}, "root[0]['key3'][0][0][0][0][1]": {'new_value': 3, 'old_value': 2, 'new_path': "root[1]['key3'][0][0][0][0][1]"}}})
])
def test_ignore_order_max_passes(self, max_passes, expected):
t1 = [
Expand Down Expand Up @@ -679,8 +679,8 @@ def test_ignore_order_max_passes(self, max_passes, expected):

@pytest.mark.parametrize('max_diffs, expected', [
(1, {}),
(65, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5'}}}),
(80, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5'}, "root[0]['key3'][0][0][0][0][1]": {'new_value': 3, 'old_value': 2}}}),
(65, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}}}),
(80, {'values_changed': {"root[1]['key5']": {'new_value': 'CHANGE', 'old_value': 'val5', 'new_path': "root[0]['key5']"}, "root[0]['key3'][0][0][0][0][1]": {'new_value': 3, 'old_value': 2, 'new_path': "root[1]['key3'][0][0][0][0][1]"}}}),
])
def test_ignore_order_max_diffs(self, max_diffs, expected):
t1 = [
Expand Down Expand Up @@ -720,8 +720,8 @@ def test_stats_that_include_distance_cache_hits(self):

diff = DeepDiff(t1, t2, ignore_order=True, cache_size=5000, cutoff_intersection_for_pairs=1)
expected = {
'PASSES COUNT': 7,
'DIFF COUNT': 37,
'PASSES COUNT': 6,
'DIFF COUNT': 33,
'DISTANCE CACHE HIT COUNT': 0,
'MAX PASS LIMIT REACHED': False,
'MAX DIFF LIMIT REACHED': False,
Expand Down

0 comments on commit d76e2e2

Please sign in to comment.