Skip to content

Commit c9adc35

Browse files
committed
DEP: Raise TypeError for subtract(bool_, bool_).
Subtracting a bool_ from a bool_ was deprecated in NumPy 1.9
1 parent ff097d4 commit c9adc35

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

doc/release/1.13.0-notes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ DeprecationWarning to error
3232

3333
* ``partition``, TypeError when non-integer partition index is used.
3434
* ``NpyIter_AdvancedNew``, ValueError when `oa_ndim == 0` and `op_axes` is NULL
35-
* Negative boolean, TypeError when unary ``-`` operator applied to boolean.
35+
* ``negative(bool_)``, TypeError when negative applied to booleans.
36+
* ``subtract(bool_, bool_)``, TypeError when subtracting boolean from boolean.
3637

3738
FutureWarning to changed behavior
3839
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

numpy/core/src/umath/ufunc_type_resolution.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -796,12 +796,11 @@ PyUFunc_SubtractionTypeResolver(PyUFuncObject *ufunc,
796796

797797
/* The type resolver would have upcast already */
798798
if (out_dtypes[0]->type_num == NPY_BOOL) {
799-
/* 2013-12-05, 1.9 */
800-
if (DEPRECATE("numpy boolean subtract, the `-` operator, is "
801-
"deprecated, use the bitwise_xor, the `^` operator, "
802-
"or the logical_xor function instead.") < 0) {
803-
return -1;
804-
}
799+
PyErr_Format(PyExc_TypeError,
800+
"numpy boolean subtract, the `-` operator, is deprecated, "
801+
"use the bitwise_xor, the `^` operator, or the logical_xor "
802+
"function instead.");
803+
return -1;
805804
}
806805
return ret;
807806
}

numpy/core/tests/test_deprecations.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,6 @@ def assert_not_deprecated(self, function, args=(), kwargs={}):
226226
exceptions=tuple(), args=args, kwargs=kwargs)
227227

228228

229-
class TestBooleanBinaryMinusDeprecation(_DeprecationTestCase):
230-
"""Test deprecation of binary boolean `-`. While + and * are well
231-
defined, binary - is not and even a corrected form seems to have
232-
no real uses.
233-
234-
The deprecation process was started in NumPy 1.9.
235-
"""
236-
message = r"numpy boolean subtract, the `-` operator, .*"
237-
238-
def test_operator_deprecation(self):
239-
array = np.array([True])
240-
generic = np.bool_(True)
241-
242-
# Minus operator/subtract ufunc:
243-
self.assert_deprecated(operator.sub, args=(array, array))
244-
self.assert_deprecated(operator.sub, args=(generic, generic))
245-
246-
247229
class TestRankDeprecation(_DeprecationTestCase):
248230
"""Test that np.rank is deprecated. The function should simply be
249231
removed. The VisibleDeprecationWarning may become unnecessary.

numpy/core/tests/test_scalarmath.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,26 @@ def test_exceptions(self):
555555
def test_result(self):
556556
types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
557557
with suppress_warnings() as sup:
558-
sup.filter(RuntimeWarning)
558+
sup.filter(RuntimeWarning)
559559
for dt in types:
560560
a = np.ones((), dtype=dt)[()]
561561
assert_equal(operator.neg(a) + a, 0)
562562

563563

564+
class TestSubtract(TestCase):
565+
def test_exceptions(self):
566+
a = np.ones((), dtype=np.bool_)[()]
567+
assert_raises(TypeError, operator.sub, a, a)
568+
569+
def test_result(self):
570+
types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
571+
with suppress_warnings() as sup:
572+
sup.filter(RuntimeWarning)
573+
for dt in types:
574+
a = np.ones((), dtype=dt)[()]
575+
assert_equal(operator.sub(a, a), 0)
576+
577+
564578
class TestAbs(TestCase):
565579

566580
def _test_abs_func(self, absfunc):

numpy/core/tests/test_umath.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ class TestBool(TestCase):
10051005
def test_exceptions(self):
10061006
a = np.ones(1, dtype=np.bool_)
10071007
assert_raises(TypeError, np.negative, a)
1008+
assert_raises(TypeError, np.subtract, a, a)
10081009

10091010
def test_truth_table_logical(self):
10101011
# 2, 3 and 4 serves as true values

0 commit comments

Comments
 (0)