Skip to content

Commit e7da467

Browse files
committed
Fix colored view display when all list items are removed
When comparing two lists where all items are removed (t2 becomes an empty list), the colored view would display an empty list [] instead of showing the removed items in red. The bug occurred because `_colorize_json()` returned '[]' immediately if the list was empty, without checking for items that were removed and should be displayed. Fix: Check for removed items before returning empty list representation. Added tests: - test_colored_view_list_all_items_removed - test_colored_compact_view_list_all_items_removed
1 parent 60ac5b9 commit e7da467

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

deepdiff/colored_view.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ def _colorize_json(self, obj: Any, path: str = 'root', indent: int = 0) -> str:
109109
return '{\n' + ',\n'.join(items) + f'\n{current_indent}' + '}'
110110

111111
elif isinstance(obj, (list, tuple)):
112-
if not obj:
113-
return '[]'
114112
removed_map = self._get_path_removed(path)
113+
if not obj and not removed_map:
114+
return '[]'
115+
115116
for index in removed_map:
116117
self._colorize_skip_paths.add(f"{path}[{index}]")
117118

tests/test_colored_view.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ def test_colored_view_list_additions():
134134
assert result == expected
135135

136136

137+
def test_colored_view_list_all_items_removed():
138+
"""Test that all removed items are displayed when list becomes empty."""
139+
t1 = [2, 4]
140+
t2 = []
141+
142+
diff = DeepDiff(t1, t2, view=COLORED_VIEW)
143+
result = str(diff)
144+
145+
expected = f'''[
146+
{RED}2{RESET},
147+
{RED}4{RESET}
148+
]'''
149+
assert result == expected
150+
151+
152+
def test_colored_compact_view_list_all_items_removed():
153+
"""Test that all removed items are displayed when list becomes empty with compact view."""
154+
t1 = [2, 4]
155+
t2 = []
156+
157+
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW)
158+
result = str(diff)
159+
160+
expected = f'''[
161+
{RED}2{RESET},
162+
{RED}4{RESET}
163+
]'''
164+
assert result == expected
165+
166+
137167
def test_colored_view_list_changes_deletions():
138168
t1 = [1, 5, 7, 3, 6]
139169
t2 = [1, 2, 3, 4]

0 commit comments

Comments
 (0)