summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2016-11-22 15:41:39 -0700
committerCharles Harris <charlesr.harris@gmail.com>2016-11-22 17:11:45 -0700
commitff097d486887ae0a8eae4f3ecc2adece1c63ac43 (patch)
tree1fc308040391e9246b671317259c5d9c5a6c72e1
parente9a7e2884a20db689fe92a9b435ff7de06d356a8 (diff)
downloadnumpy-ff097d486887ae0a8eae4f3ecc2adece1c63ac43.tar.gz
DEP: Raise TypeError for np.negative(bool_).
The unary minus of booleans was deprecated in NumPy 1.9.
-rw-r--r--doc/release/1.13.0-notes.rst1
-rw-r--r--numpy/core/src/umath/ufunc_type_resolution.c18
-rw-r--r--numpy/core/tests/test_deprecations.py18
-rw-r--r--numpy/core/tests/test_scalarmath.py14
-rw-r--r--numpy/core/tests/test_umath.py4
5 files changed, 27 insertions, 28 deletions
diff --git a/doc/release/1.13.0-notes.rst b/doc/release/1.13.0-notes.rst
index 34026c4e5..a8a63018c 100644
--- a/doc/release/1.13.0-notes.rst
+++ b/doc/release/1.13.0-notes.rst
@@ -32,6 +32,7 @@ DeprecationWarning to error
* ``partition``, TypeError when non-integer partition index is used.
* ``NpyIter_AdvancedNew``, ValueError when `oa_ndim == 0` and `op_axes` is NULL
+* Negative boolean, TypeError when unary ``-`` operator applied to boolean.
FutureWarning to changed behavior
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c
index 50e020386..247526a54 100644
--- a/numpy/core/src/umath/ufunc_type_resolution.c
+++ b/numpy/core/src/umath/ufunc_type_resolution.c
@@ -368,10 +368,10 @@ PyUFunc_SimpleUnaryOperationTypeResolver(PyUFuncObject *ufunc,
NPY_NO_EXPORT int
PyUFunc_NegativeTypeResolver(PyUFuncObject *ufunc,
- NPY_CASTING casting,
- PyArrayObject **operands,
- PyObject *type_tup,
- PyArray_Descr **out_dtypes)
+ NPY_CASTING casting,
+ PyArrayObject **operands,
+ PyObject *type_tup,
+ PyArray_Descr **out_dtypes)
{
int ret;
ret = PyUFunc_SimpleUnaryOperationTypeResolver(ufunc, casting, operands,
@@ -382,12 +382,10 @@ PyUFunc_NegativeTypeResolver(PyUFuncObject *ufunc,
/* The type resolver would have upcast already */
if (out_dtypes[0]->type_num == NPY_BOOL) {
- /* 2013-12-05, 1.9 */
- if (DEPRECATE("numpy boolean negative, the `-` operator, is "
- "deprecated, use the `~` operator or the logical_not "
- "function instead.") < 0) {
- return -1;
- }
+ PyErr_Format(PyExc_TypeError,
+ "The numpy boolean negative, the `-` operator, is not supported, "
+ "use the `~` operator or the logical_not function instead.");
+ return -1;
}
return ret;
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index 09c00e944..49fe49be9 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -226,24 +226,6 @@ class _DeprecationTestCase(object):
exceptions=tuple(), args=args, kwargs=kwargs)
-class TestBooleanUnaryMinusDeprecation(_DeprecationTestCase):
- """Test deprecation of unary boolean `-`. While + and * are well
- defined, unary - is not and even a corrected form seems to have
- no real uses.
-
- The deprecation process was started in NumPy 1.9.
- """
- message = r"numpy boolean negative, the `-` operator, .*"
-
- def test_unary_minus_operator_deprecation(self):
- array = np.array([True])
- generic = np.bool_(True)
-
- # Unary minus/negative ufunc:
- self.assert_deprecated(operator.neg, args=(array,))
- self.assert_deprecated(operator.neg, args=(generic,))
-
-
class TestBooleanBinaryMinusDeprecation(_DeprecationTestCase):
"""Test deprecation of binary boolean `-`. While + and * are well
defined, binary - is not and even a corrected form seems to have
diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py
index b39ec6db2..33713b986 100644
--- a/numpy/core/tests/test_scalarmath.py
+++ b/numpy/core/tests/test_scalarmath.py
@@ -547,6 +547,20 @@ class TestMultiply(TestCase):
assert_array_equal(np.int_(3) * arr_like, np.full(3, 3))
+class TestNegative(TestCase):
+ def test_exceptions(self):
+ a = np.ones((), dtype=np.bool_)[()]
+ assert_raises(TypeError, operator.neg, a)
+
+ def test_result(self):
+ types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
+ with suppress_warnings() as sup:
+ sup.filter(RuntimeWarning)
+ for dt in types:
+ a = np.ones((), dtype=dt)[()]
+ assert_equal(operator.neg(a) + a, 0)
+
+
class TestAbs(TestCase):
def _test_abs_func(self, absfunc):
diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py
index 4c0243559..0275d10c6 100644
--- a/numpy/core/tests/test_umath.py
+++ b/numpy/core/tests/test_umath.py
@@ -1002,6 +1002,10 @@ class TestFmin(_FilterInvalids):
class TestBool(TestCase):
+ def test_exceptions(self):
+ a = np.ones(1, dtype=np.bool_)
+ assert_raises(TypeError, np.negative, a)
+
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]