10000 BUG: None comparison deprecation does not affect scalars by seberg · Pull Request #4910 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: None comparison deprecation does not affect scalars #4910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
  • Loading branch information
seberg committed Jul 28, 2014
commit b15635b2dc2cc98065913e4c45a5139cada38b77
18 changes: 18 additions & 0 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,24 @@ gentype_richcompare(PyObject *self, PyObject *other, int cmp_op)
{
PyObject *arr, *ret;

/*
* If the other object is None, False is always right. This avoids
* the array None comparison, at least until deprecation it is fixed.
* After that, this may be removed and numpy false would be returned.
*
* NOTE: np.equal(NaT, None) evaluates to TRUE! This is an
* an inconsistency, which may has to be considered
* when the deprecation is finished.
*/
if (other == Py_None) {
if (cmp_op == Py_EQ) {
Py_RETURN_FALSE;
}
if (cmp_op == Py_NE) {
Py_RETURN_TRUE;
}
}

arr = PyArray_FromScalar(self, NULL);
if (arr == NULL) {
return NULL;
Expand Down
22 changes: 21 additions & 1 deletion numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import numpy as np
from numpy.testing import (dec, run_module_suite, assert_raises,
assert_warns, assert_array_equal)
assert_warns, assert_array_equal, assert_)


class _DeprecationTestCase(object):
Expand Down Expand Up @@ -426,6 +426,26 @@ def test_none_comparison(self):
assert_raises(FutureWarning, operator.eq, np.arange(3), None)
assert_raises(FutureWarning, operator.ne, np.arange(3), None)

def test_scalar_none_comparison(self):
# Scalars should still just return false and not give a warnings.
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', FutureWarning)
assert_(not np.float32(1) == None)
assert_(not np.str_('test') == None)
# This is dubious (see below):
assert_(not np.datetime64('NaT') == None)

assert_(np.float32(1) != None)
assert_(np.str_('test') != None)
# This is dubious (see below):
assert_(np.datetime64('NaT') != None)
assert_(len(w) == 0)

# For documentaiton purpose, this is why the datetime is dubious.
# At the time of deprecation this was no behaviour change, but
# it has to be considered when the deprecations is done.
assert_(np.equal(np.datetime64('NaT'), None))


class TestIdentityComparisonDepreactions(_DeprecationTestCase):
"""This tests the equal and not_equal object ufuncs identity check
Expand Down
0