From e7da467d708ec767f63f06aac94fc410aa07e38a Mon Sep 17 00:00:00 2001 From: Yann Rouillard Date: Tue, 4 Nov 2025 20:32:32 +0100 Subject: [PATCH] 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 --- deepdiff/colored_view.py | 5 +++-- tests/test_colored_view.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/deepdiff/colored_view.py b/deepdiff/colored_view.py index 2463dcbf..5f6c6239 100644 --- a/deepdiff/colored_view.py +++ b/deepdiff/colored_view.py @@ -109,9 +109,10 @@ def _colorize_json(self, obj: Any, path: str = 'root', indent: int = 0) -> str: return '{\n' + ',\n'.join(items) + f'\n{current_indent}' + '}' elif isinstance(obj, (list, tuple)): - if not obj: - return '[]' removed_map = self._get_path_removed(path) + if not obj and not removed_map: + return '[]' + for index in removed_map: self._colorize_skip_paths.add(f"{path}[{index}]") diff --git a/tests/test_colored_view.py b/tests/test_colored_view.py index 3c8c1a5b..b2e8a613 100644 --- a/tests/test_colored_view.py +++ b/tests/test_colored_view.py @@ -134,6 +134,36 @@ def test_colored_view_list_additions(): assert result == expected +def test_colored_view_list_all_items_removed(): + """Test that all removed items are displayed when list becomes empty.""" + t1 = [2, 4] + t2 = [] + + diff = DeepDiff(t1, t2, view=COLORED_VIEW) + result = str(diff) + + expected = f'''[ + {RED}2{RESET}, + {RED}4{RESET} +]''' + assert result == expected + + +def test_colored_compact_view_list_all_items_removed(): + """Test that all removed items are displayed when list becomes empty with compact view.""" + t1 = [2, 4] + t2 = [] + + diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) + result = str(diff) + + expected = f'''[ + {RED}2{RESET}, + {RED}4{RESET} +]''' + assert result == expected + + def test_colored_view_list_changes_deletions(): t1 = [1, 5, 7, 3, 6] t2 = [1, 2, 3, 4]