Skip to content

Commit 18a0334

Browse files
committed
Fix bool evaluation
1 parent 77fa8d3 commit 18a0334

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

deepdiff/diff.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,11 @@ def affected_root_keys(self):
19071907
result.add(root_key)
19081908
return result
19091909

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+
19101915
def __str__(self):
19111916
if hasattr(self, '_colored_view') and self.view in {COLORED_VIEW, COLORED_COMPACT_VIEW}:
19121917
return str(self._colored_view)

tests/test_colored_view.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,28 @@ def test_compact_view_primitive_siblings():
309309
"nested_sibling": {{...}}
310310
}}'''
311311
assert result == expected
312+
313+
314+
def test_colored_view_bool_evaluation():
315+
# Test COLORED_VIEW
316+
# Scenario 1: No differences
317+
t1_no_diff = {"a": 1, "b": 2}
318+
t2_no_diff = {"a": 1, "b": 2}
319+
diff_no_diff_colored = DeepDiff(t1_no_diff, t2_no_diff, view=COLORED_VIEW)
320+
assert not bool(diff_no_diff_colored), "bool(diff) should be False when no diffs (colored view)"
321+
322+
# Scenario 2: With differences
323+
t1_with_diff = {"a": 1, "b": 2}
324+
t2_with_diff = {"a": 1, "b": 3}
325+
diff_with_diff_colored = DeepDiff(t1_with_diff, t2_with_diff, view=COLORED_VIEW)
326+
assert bool(diff_with_diff_colored), "bool(diff) should be True when diffs exist (colored view)"
327+
328+
# Test COLORED_COMPACT_VIEW
329+
# Scenario 1: No differences
330+
diff_no_diff_compact = DeepDiff(t1_no_diff, t2_no_diff, view=COLORED_COMPACT_VIEW)
331+
assert not bool(diff_no_diff_compact), "bool(diff) should be False when no diffs (compact view)"
332+
333+
# Scenario 2: With differences
334+
diff_with_diff_compact = DeepDiff(t1_with_diff, t2_with_diff, view=COLORED_COMPACT_VIEW)
335+
assert bool(diff_with_diff_compact), "bool(diff) should be True when diffs exist (compact view)"
336+

0 commit comments

Comments
 (0)