Skip to content
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

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

Closed
radoering opened this issue Jul 20, 2024 · 2 comments · Fixed by #12473
Closed

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

radoering opened this issue Jul 20, 2024 · 2 comments · Fixed by #12473
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@radoering
Copy link

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.

@radoering radoering changed the title NPY201: replacing sometrue/alltrue with any/all results in invalid code NPY201: replacing sometrue/alltrue with any/all results in broken code Jul 20, 2024
@charliermarsh charliermarsh added bug Something isn't working fixes Related to suggested fixes for violations labels Jul 20, 2024
@charliermarsh
Copy link
Member

@mtsokol -- Do you see any issue with this change?

@mtsokol
Copy link
Contributor

mtsokol commented Jul 23, 2024

Right, it's clearly a mistake. It should use NumPy's functions np.any and np.all. Fixed in #12473.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants