Skip to content

Commit 8dcb758

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 8dcb758

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

deepdiff/colored_view.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ 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 '[]'
112+
# Check for removed items BEFORE returning empty list
114113
removed_map = self._get_path_removed(path)
114+
115+
# If list is empty AND no removed items, return []
116+
if not obj and not removed_map:
117+
return '[]'
118+
119+
# Mark removed paths to skip
115120
for index in removed_map:
116121
self._colorize_skip_paths.add(f"{path}[{index}]")
117122

tests/test_colored_view.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,42 @@ 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 = [
140+
{"kind": "ServiceAccount", "name": "test-sa"},
141+
{"kind": "Deployment", "name": "test-deploy"}
142+
]
143+
t2 = []
144+
145+
diff = DeepDiff(t1, t2, view=COLORED_VIEW)
146+
result = str(diff)
147+
148+
expected = f'''[
149+
{RED}{{"kind": "ServiceAccount", "name": "test-sa"}}{RESET},
150+
{RED}{{"kind": "Deployment", "name": "test-deploy"}}{RESET}
151+
]'''
152+
assert result == expected
153+
154+
155+
def test_colored_compact_view_list_all_items_removed():
156+
"""Test that all removed items are displayed when list becomes empty with compact view."""
157+
t1 = [
158+
{"kind": "ServiceAccount", "name": "test-sa"},
159+
{"kind": "Deployment", "name": "test-deploy"}
160+
]
161+
t2 = []
162+
163+
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW)
164+
result = str(diff)
165+
166+
expected = f'''[
167+
{RED}{{"kind": "ServiceAccount", "name": "test-sa"}}{RESET},
168+
{RED}{{"kind": "Deployment", "name": "test-deploy"}}{RESET}
169+
]'''
170+
assert result == expected
171+
172+
137173
def test_colored_view_list_changes_deletions():
138174
t1 = [1, 5, 7, 3, 6]
139175
t2 = [1, 2, 3, 4]

0 commit comments

Comments
 (0)