Skip to content

Commit 3796f9b

Browse files
committed
FIX: raise error in ExprNode.__bool__ instead of returning True
1 parent 613a522 commit 3796f9b

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/changes/version_0_34_2.rst.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ Fixes
6262
* Array.split_axes now raises an explicit error when some labels contain
6363
more separators than others, instead of silently dropping part of those
6464
labels, or even some data (closes :issue:`1089`).
65+
66+
* a boolean condition including only X.axes_name and scalars (e.g. X.age == 0)
67+
raises an error when Python needs to know whether it is True or not (because
68+
there is no array to extract the axis labels from) instead of always
69+
evaluating to True. This was especially dangerous in the context of a
70+
where() function, which always evaluated to its left side
71+
(e.g. where(X.age > 0, arr, 0) evaluated to 'arr' for all ages).
72+
Closes :issue:`1083`.

larray/core/expr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class ExprNode:
2+
def __bool__(self):
3+
raise ValueError("Cannot evaluate the truth value of an expression using X.axis_name")
4+
25
# method factory
36
def _binop(opname):
47
def opmethod(self, other):

larray/tests/test_array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,11 @@ def test_unary_ops(small_array):
29452945
assert_nparray_equal((~small_array).data, ~raw)
29462946

29472947

2948+
def test_binary_ops_expressions():
2949+
with must_raise(ValueError, "Cannot evaluate the truth value of an expression using X.axis_name"):
2950+
res = 0 if X.age == 3 else 1
2951+
2952+
29482953
def test_mean(small_array):
29492954
raw = small_array.data
29502955
c, d = small_array.axes

0 commit comments

Comments
 (0)