Skip to content

NPY201: replacing sometrue/alltrue with any/all results in broken code #12416

@radoering

Description

@radoering

NPY201 can produce broken code. Replacing numpy.sometrue with any is dangerous and does not work for multidimensional arrays. Using numpy.any instead is probably a better option.

numpy 1.26.4:

>>> import numpy as np
>>> a = np.array([[False, False], [False, False]])
>>> np.sometrue(a)
False
>>> np.any(a)
False
>>> any(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

numpy 2.0:

>>> import numpy as np
>>> a = np.array([[False, False], [False, False]])
>>> np.any(a)
np.False_
>>> any(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Probably the same for numpy.alltrue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions