Skip to content

Commit 82f890c

Browse files
committed
Fixes and more tests
1 parent 18a0334 commit 82f890c

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

deepdiff/colored_view.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import json
2+
import os
23
from ast import literal_eval
34
from importlib.util import find_spec
45
from typing import Any, Dict
56

6-
if find_spec("colorama"):
7+
if os.name == "nt" and find_spec("colorama"):
78
import colorama
89

910
colorama.init()
@@ -14,6 +15,7 @@
1415
GREEN = '\033[32m'
1516
RESET = '\033[0m'
1617

18+
1719
class ColoredView:
1820
"""A view that shows JSON with color-coded differences."""
1921

@@ -127,7 +129,7 @@ def _colorize_json(self, obj: Any, path: str = 'root', indent: int = 0) -> str:
127129
def __str__(self) -> str:
128130
"""Return the colorized, pretty-printed JSON string."""
129131
self._colorize_skip_paths = set()
130-
return self._colorize_json(self.t2, indent=0)
132+
return self._colorize_json(self.t2)
131133

132134
def __iter__(self):
133135
"""Make the view iterable by yielding the tree results."""

deepdiff/commands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def cli():
5454
@click.option('--significant-digits', required=False, default=None, type=int, show_default=True)
5555
@click.option('--truncate-datetime', required=False, type=click.Choice(['second', 'minute', 'hour', 'day'], case_sensitive=True), show_default=True, default=None)
5656
@click.option('--verbose-level', required=False, default=1, type=click.IntRange(0, 2), show_default=True)
57-
@click.option('--view', required=False, type=click.Choice(['tree', 'colored', 'colored_compact'], case_sensitive=True), show_default=True, default="tree")
57+
@click.option('--view', required=False, type=click.Choice(['tree', 'colored', 'colored_compact'], case_sensitive=True))
5858
@click.option('--debug', is_flag=True, show_default=False)
5959
def diff(
6060
*args, **kwargs
@@ -75,6 +75,8 @@ def diff(
7575
t2_path = kwargs.pop("t2")
7676
t1_extension = t1_path.split('.')[-1]
7777
t2_extension = t2_path.split('.')[-1]
78+
if "view" in kwargs and kwargs["view"] is None:
79+
kwargs.pop("view")
7880

7981
for name, t_path, t_extension in [('t1', t1_path, t1_extension), ('t2', t2_path, t2_extension)]:
8082
try:

deepdiff/diff.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
type_is_subclass_of_type_group, type_in_type_group, get_doc,
2626
number_to_string, datetime_normalize, KEY_TO_VAL_STR, booleans,
2727
np_ndarray, np_floating, get_numpy_ndarray_rows, RepeatedTimer,
28-
TEXT_VIEW, TREE_VIEW, DELTA_VIEW, COLORED_VIEW, COLORED_COMPACT_VIEW, __dict__, add_root_to_paths,
28+
TEXT_VIEW, TREE_VIEW, DELTA_VIEW, COLORED_VIEW, COLORED_COMPACT_VIEW,
29+
detailed__dict__, add_root_to_paths,
2930
np, get_truncate_datetime, dict_, CannotCompare, ENUM_INCLUDE_KEYS,
3031
PydanticBaseModel, Opcode, SetOrdered, ipranges)
3132
from deepdiff.serialization import SerializationMixin
@@ -366,7 +367,8 @@ def _group_by_sort_key(x):
366367

367368
self.tree.remove_empty_keys()
368369
view_results = self._get_view_results(self.view)
369-
if self.view in {COLORED_VIEW, COLORED_COMPACT_VIEW}:
370+
if isinstance(view_results, ColoredView):
371+
self.update(view_results.tree)
370372
self._colored_view = view_results
371373
else:
372374
self.update(view_results)
@@ -1907,11 +1909,6 @@ def affected_root_keys(self):
19071909
result.add(root_key)
19081910
return result
19091911

1910-
def __bool__(self):
1911-
if hasattr(self, '_colored_view') and self.view in {COLORED_VIEW, COLORED_COMPACT_VIEW}:
1912-
return bool(self.tree) # Use the tree for boolean evaluation, not the view
1913-
return super().__bool__()
1914-
19151912
def __str__(self):
19161913
if hasattr(self, '_colored_view') and self.view in {COLORED_VIEW, COLORED_COMPACT_VIEW}:
19171914
return str(self._colored_view)

tests/test_colored_view.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from deepdiff.helper import COLORED_VIEW, COLORED_COMPACT_VIEW
33
from deepdiff.colored_view import RED, GREEN, RESET
44

5+
56
def test_colored_view_basic():
67
t1 = {
78
"name": "John",
@@ -45,6 +46,7 @@ def test_colored_view_basic():
4546
}}'''
4647
assert result == expected
4748

49+
4850
def test_colored_view_nested_changes():
4951
t1 = {
5052
"level1": {
@@ -80,6 +82,7 @@ def test_colored_view_nested_changes():
8082
}}'''
8183
assert result == expected
8284

85+
8386
def test_colored_view_list_changes():
8487
t1 = [1, 2, 3, 4]
8588
t2 = [1, 5, 3, 6]
@@ -95,6 +98,7 @@ def test_colored_view_list_changes():
9598
]'''
9699
assert result == expected
97100

101+
98102
def test_colored_view_list_deletions():
99103
t1 = [1, 2, 3, 4, 5, 6]
100104
t2 = [2, 4]
@@ -112,7 +116,59 @@ def test_colored_view_list_deletions():
112116
]'''
113117
assert result == expected
114118

115-
def test_colored_view_with_ignore_order():
119+
120+
def test_colored_view_list_additions():
121+
t1 = [2, 4]
122+
t2 = [1, 2, 3, 4, 5]
123+
124+
diff = DeepDiff(t1, t2, view=COLORED_VIEW)
125+
result = str(diff)
126+
127+
expected = f'''[
128+
{GREEN}1{RESET},
129+
2,
130+
{GREEN}3{RESET},
131+
4,
132+
{GREEN}5{RESET}
133+
]'''
134+
assert result == expected
135+
136+
137+
def test_colored_view_list_changes_deletions():
138+
t1 = [1, 5, 7, 3, 6]
139+
t2 = [1, 2, 3, 4]
140+
141+
diff = DeepDiff(t1, t2, view=COLORED_VIEW)
142+
result = str(diff)
143+
144+
expected = f'''[
145+
1,
146+
{RED}5{RESET} -> {GREEN}2{RESET},
147+
{RED}7{RESET},
148+
3,
149+
{RED}6{RESET} -> {GREEN}4{RESET}
150+
]'''
151+
assert result == expected
152+
153+
154+
def test_colored_view_list_changes_additions():
155+
t1 = [1, 2, 3, 4]
156+
t2 = [1, 5, 7, 3, 6]
157+
158+
diff = DeepDiff(t1, t2, view=COLORED_VIEW)
159+
result = str(diff)
160+
161+
expected = f'''[
162+
1,
163+
{RED}2{RESET} -> {GREEN}5{RESET},
164+
{GREEN}7{RESET},
165+
3,
166+
{RED}4{RESET} -> {GREEN}6{RESET}
167+
]'''
168+
assert result == expected
169+
170+
171+
def test_colored_view_list_no_changes_with_ignore_order():
116172
t1 = [1, 2, 3]
117173
t2 = [3, 2, 1]
118174

@@ -126,7 +182,8 @@ def test_colored_view_with_ignore_order():
126182
]'''
127183
assert result == expected
128184

129-
def test_colored_view_with_empty_diff():
185+
186+
def test_colored_view_no_changes():
130187
t1 = {"a": 1, "b": 2}
131188
t2 = {"a": 1, "b": 2}
132189

@@ -139,6 +196,7 @@ def test_colored_view_with_empty_diff():
139196
}'''
140197
assert result == expected
141198

199+
142200
def test_compact_view_basic():
143201
t1 = {
144202
"name": "John",
@@ -194,6 +252,7 @@ def test_compact_view_basic():
194252
}}'''
195253
assert result == expected
196254

255+
197256
def test_compact_view_nested_changes():
198257
t1 = {
199258
"level1": {
@@ -251,6 +310,7 @@ def test_compact_view_nested_changes():
251310
}}'''
252311
assert result == expected
253312

313+
254314
def test_compact_view_no_changes():
255315
# Test with dict
256316
t1 = {"a": 1, "b": [1, 2], "c": {"x": True}}
@@ -264,6 +324,7 @@ def test_compact_view_no_changes():
264324
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW)
265325
assert str(diff) == "[...]"
266326

327+
267328
def test_compact_view_list_changes():
268329
t1 = [1, {"a": 1, "b": {"x": 1, "y": 2}}, [1, 2, {"z": 3}]]
269330
t2 = [1, {"a": 2, "b": {"x": 1, "y": 2}}, [1, 2, {"z": 3}]]
@@ -281,6 +342,7 @@ def test_compact_view_list_changes():
281342
]'''
282343
assert result == expected
283344

345+
284346
def test_compact_view_primitive_siblings():
285347
t1 = {
286348
"changed": 1,
@@ -333,4 +395,3 @@ def test_colored_view_bool_evaluation():
333395
# Scenario 2: With differences
334396
diff_with_diff_compact = DeepDiff(t1_with_diff, t2_with_diff, view=COLORED_COMPACT_VIEW)
335397
assert bool(diff_with_diff_compact), "bool(diff) should be True when diffs exist (compact view)"
336-

0 commit comments

Comments
 (0)