Skip to content

Elementwise equality comparison with zero-length void dtype fails on NumPy 1.21 #19305

Closed

Description

@shoyer

Reproducing code example:

With NumPy 1.21:

>>> import numpy as np
>>> x = np.zeros((3,), np.void)
>>> x == x
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: FutureWarning: elementwise == comparison failed and returning scalar instead; this will raise an error or perform elementwise comparison in the future.
False

I believe the error comes from these lines:

_res = PyArray_CanCastTypeTo(PyArray_DESCR(self),
PyArray_DESCR(array_other),
NPY_EQUIV_CASTING);
if (_res == 0) {
/* 2015-05-07, 1.10 */
Py_DECREF(array_other);
if (DEPRECATE_FUTUREWARNING(
"elementwise == comparison failed and returning scalar "
"instead; this will raise an error or perform "
"elementwise comparison in the future.") < 0) {
return NULL;
}
Py_INCREF(Py_False);
return Py_False;
}

On NumPy 1.20, the output is correct:

array([ True,  True,  True])

Or conversely, if we use a non-zero length void dtype, e.g.,

>>> x = np.array([b'foo', b'bar'], np.void)
>>> x == x
array([ True,  True])

CC @seberg since this smells similar to #19085

Activity

  1. seberg commented on Jun 23, 2021

    @seberg
    Member

    Probably should just fix this local to comparisons, unless you found the issue also in another context? It is not like you can do a whole lot with voids, especially empty ones...

  2. shoyer commented on Jun 23, 2021

    @shoyer
    MemberAuthor

    We have only encountered this in comparisons, so that would work for us.

    The empty void dtype is convenient for JAX as a logical "placeholder" for arrays with a particular shape that doesn't require allocating memory (the arrays are always zero).

  3. seberg commented on Jun 24, 2021

    @seberg
    Member

    Would be nice if you can test gh-19322, just to be sure.

  4. hawkinsp commented on Jun 24, 2021

    @hawkinsp
    Contributor

    @seberg I can confirm that NumPy built at your PR gh-19322 fixes the test failures we saw in the JAX test suite with NumPy 1.21.

    Any chance we can get this fix into 1.21.1 if/when that is a thing?

  5. seberg commented on Jun 24, 2021

    @seberg
    Member

    Yes, that PR is practically backport only, the whole branch should use different logic in 1.22/2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions