Skip to content

Commit

Permalink
fixes accessing the affected_root_keys property on the diff object re…
Browse files Browse the repository at this point in the history
…turned by DeepDiff fails when one of the dicts is empty #508
  • Loading branch information
seperman committed Dec 15, 2024
1 parent 42fd42d commit c464e04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 6 additions & 2 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,9 +1846,13 @@ def affected_root_keys(self):
value = self.tree.get(key)
if value:
if isinstance(value, SetOrdered):
result |= SetOrdered([i.get_root_key() for i in value])
values_list = value
else:
result |= SetOrdered([i.get_root_key() for i in value.keys()])
values_list = value.keys()
for item in values_list:
root_key = item.get_root_key()
if root_key is not notpresent:
result.add(root_key)
return result


Expand Down
4 changes: 3 additions & 1 deletion deepdiff/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,9 @@ def get_root_key(self, use_t2=False):
else:
next_rel = root_level.t1_child_rel or root_level.t2_child_rel # next relationship object to get a formatted param from

return next_rel.param
if next_rel:
return next_rel.param
return notpresent

def path(self, root="root", force=None, get_parent_too=False, use_t2=False, output_format='str'):
"""
Expand Down
25 changes: 25 additions & 0 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,24 @@ class ClassB:
result = {'attribute_removed': ['root.y']}
assert result == ddiff

def test_custom_objects_slot_in_group_change(self):
class ClassA:
__slots__ = ('x', 'y')

def __init__(self, x, y):
self.x = x
self.y = y

class ClassB(ClassA):
pass

t1 = ClassA(1, 1)
t2 = ClassB(1, 1)
ddiff = DeepDiff(t1, t2, ignore_type_in_groups=[(ClassA, ClassB)])
result = {}
assert result == ddiff


def test_custom_class_changes_none_when_ignore_type(self):
ddiff1 = DeepDiff({'a': None}, {'a': 1}, ignore_type_subclasses=True, ignore_type_in_groups=[(int, float)])
result = {
Expand Down Expand Up @@ -2226,3 +2244,10 @@ def test_group_by_with_none_key_and_ignore_case(self):
}
}
assert expected == diff

def test_affected_root_keys_when_dict_empty(self):
diff = DeepDiff({}, {1:1, 2:2}, threshold_to_diff_deeper=0)
assert [1, 2] == diff.affected_root_keys

diff2 = DeepDiff({}, {1:1, 2:2})
assert [] == diff2.affected_root_keys

0 comments on commit c464e04

Please sign in to comment.