Skip to content

Commit ffa7029

Browse files
authored
Merge pull request #573 from yannrouillard/fix/colored-view-empty-list-display
Fix: Colored view displays empty list [] when all items are removed
2 parents edded17 + e7da467 commit ffa7029

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

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)