10000 Merge pull request #4910 from seberg/scalar-none-cmp · numpy/numpy@84a9ab0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 84a9ab0

Browse files
committed
Merge pull request #4910 from seberg/scalar-none-cmp
BUG: None comparison deprecation does not affect scalars
2 parents bc4a583 + 8624924 commit 84a9ab0

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,24 @@ gentype_richcompare(PyObject *self, PyObject *other, int cmp_op)
10781078
{
10791079
PyObject *arr, *ret;
10801080

1081+
/*
1082+
* If the other object is None, False is always right. This avoids
1083+
* the array None comparison, at least until deprecation it is fixed.
1084+
* After that, this may be removed and numpy false would be returned.
1085+
*
1086+
* NOTE: np.equal(NaT, None) evaluates to TRUE! This is an
1087+
* an inconsistency, which may has to be considered
1088+
* when the deprecation is finished.
1089+
*/
1090+
if (other == Py_None) {
1091+
if (cmp_op == Py_EQ) {
1092+
Py_RETURN_FALSE;
1093+
}
1094+
if (cmp_op == Py_NE) {
1095+
Py_RETURN_TRUE;
1096+
}
1097+
}
1098+
10811099
arr = PyArray_FromScalar(self, NULL);
10821100
if (arr == NULL) {
10831101
return NULL;

numpy/core/tests/test_deprecations.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import numpy as np
1414
from numpy.testing import (dec, run_module_suite, assert_raises,
15-
assert_warns, assert_array_equal)
15+
assert_warns, assert_array_equal, assert_)
1616

1717

1818
class _DeprecationTestCase(object):
@@ -434,6 +434,26 @@ def test_none_comparison(self):
434434
assert_raises(FutureWarning, operator.eq, np.arange(3), None)
435435
assert_raises(FutureWarning, operator.ne, np.arange(3), None)
436436

437+
def test_scalar_none_comparison(self):
438+
# Scalars should still just return false and not give a warnings.
439+
with warnings.catch_warnings(record=True) as w:
440+
warnings.filterwarnings('always', '', FutureWarning)
441+
assert_(not np.float32(1) == None)
442+
assert_(not np.str_('test') == None)
443+
# This is dubious (see below):
444+
assert_(not np.datetime64('NaT') == None)
445+
446+
assert_(np.float32(1) != None)
447+
assert_(np.str_('test') != None)
448+
# This is dubious (see below):
449+
assert_(np.datetime64('NaT') != None)
450+
assert_(len(w) == 0)
451+
452+
# For documentaiton purpose, this is why the datetime is dubious.
453+
# At the time of deprecation this was no behaviour change, but
454+
# it has to be considered when the deprecations is done.
455+
assert_(np.equal(np.datetime64('NaT'), None))
456+
437457

438458
class TestIdentityComparisonDepreactions(_DeprecationTestCase):
439459
"""This tests the equal and not_equal object ufuncs identity check

0 commit comments

Comments
 (0)
0