8000 BUG: None comparison deprecation does not affect scalars · numpy/numpy@8624924 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8624924

Browse files
sebergjuliantaylor
authored andcommitted
BUG: None comparison deprecation does not affect scalars
This circumvents the None warnings for scalar comparisons. Unfortunatly datetime NaT *can* in some cases evaluate equal to None because NaT.item() is None. Closes gh-4894
1 parent 638e627 commit 8624924

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):
@@ -426,6 +426,26 @@ def test_none_comparison(self):
426426
assert_raises(FutureWarning, operator.eq, np.arange(3), None)
427427
assert_raises(FutureWarning, operator.ne, np.arange(3), None)
428428

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

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

0 commit comments

Comments
 (0)
0