8000 Merge pull request #27160 from seberg/dep-nonzero · numpy/numpy@416bc19 · GitHub
[go: up one dir, main page]

Skip to content

Commit 416bc19

Browse files
authored
Merge pull request #27160 from seberg/dep-nonzero
DEP: Finalize ``bool(empty_array)`` deprecation
2 parents 89b6820 + 0525bb0 commit 416bc19

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* ``bool(np.array([]))`` and other empty arrays will now raise an error.
2+
Use ``arr.size > 0`` instead to check whether an array has no elements.

doc/source/reference/arrays.ndarray.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,11 @@ Truth value of an array (:class:`bool() <bool>`):
467467

468468
Truth-value testing of an array invokes
469469
:meth:`ndarray.__bool__`, which raises an error if the number of
470-
elements in the array is larger than 1, because the truth value
470+
elements in the array is not 1, because the truth value
471471
of such arrays is ambiguous. Use :meth:`.any() <ndarray.any>` and
472472
:meth:`.all() <ndarray.all>` instead to be clear about what is meant
473-
in such cases. (If the number of elements is 0, the array evaluates
474-
to ``False``.)
473+
in such cases. (If you wish to check for whether an array is empty,
474+
use for example ``.size > 0``.)
475475

476476

477477
Unary operations:

numpy/_core/src/multiarray/number.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,10 @@ _array_nonzero(PyArrayObject *mp)
755755
return res;
756756
}
757757
else if (n == 0) {
758-
/* 2017-09-25, 1.14 */
759-
if (DEPRECATE("The truth value of an empty array is ambiguous. "
760-
"Returning False, but in future this will result in an error. "
761-
"Use `array.size > 0` to check that an array is not empty.") < 0) {
762-
return -1;
763-
}
764-
return 0;
758+
PyErr_SetString(PyExc_ValueError,
759+
"The truth value of an empty array is ambiguous. "
760+
"Use `array.size > 0` to check that an array is not empty.");
761+
return -1;
765762
}
766763
else {
767764
PyErr_SetString(PyExc_ValueError,

numpy/_core/tests/test_deprecations.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,6 @@ def test_3_tuple(self):
199199
self.assert_deprecated(cls, args=(1, ('ms', 2, 1, 63)))
200200

201201

202-
class TestTruthTestingEmptyArrays(_DeprecationTestCase):
203-
# 2017-09-25, 1.14.0
204-
message = '.*truth value of an empty array is ambiguous.*'
205-
206-
def test_1d(self):
207-
self.assert_deprecated(bool, args=(np.array([]),))
208-
209-
def test_2d(self):
210-
self.assert_deprecated(bool, args=(np.zeros((1, 0)),))
211-
self.assert_deprecated(bool, args=(np.zeros((0, 1)),))
212-
self.assert_deprecated(bool, args=(np.zeros((0, 0)),))
213-
214-
215202
class TestBincount(_DeprecationTestCase):
216203
# 2017-06-01, 1.14.0
217204
def test_bincount_minlength(self):

numpy/_core/tests/test_multiarray.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8900,7 +8900,8 @@ def test_to_bool_scalar(self):
89008900
assert_equal(bool(np.array([False])), False)
89018901
assert_equal(bool(np.array([True])), True)
89028902
assert_equal(bool(np.array([[42]])), True)
8903-
assert_raises(ValueError, bool, np.array([1, 2]))
8903+
8904+
def test_to_bool_scalar_not_convertible(self):
89048905

89058906
class NotConvertible:
89068907
def __bool__(self):
@@ -8919,6 +8920,16 @@ def __bool__(self):
89198920
assert_raises(Error, bool, self_containing) # previously stack overflow
89208921
self_containing[0] = None # resolve circular reference
89218922

8923+
def test_to_bool_scalar_size_errors(self):
8924+
with pytest.raises(ValueError, match=".*one element is ambiguous"):
8925+
bool(np.array([1, 2]))
8926+
8927+
with pytest.raises(ValueError, match=".*empty array is ambiguous"):
8928+
bool(np.empty((3, 0)))
8929+
8930+
with pytest.raises(ValueError, match=".*empty array is ambiguous"):
8931+
bool(np.empty((0,)))
8932+
89228933
def test_to_int_scalar(self):
89238934
# gh-9972 means that these aren't always the same
89248935
int_funcs = (int, lambda x: x.__int__())

0 commit comments

Comments
 (0)
0