numpy/compare-reductions
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | error | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
a.all() == b.all() compares two single booleans, so it is True for many arrays that differ. Use np.array_equal(a, b).
same = a.all() == b.all()same = np.array_equal(a, b)What the rule reports
Section titled “What the rule reports”The rule reports a.all() == b.all() and np.any(a) == np.any(b), which compare two single booleans and not the arrays.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes np.array_equal(a, b).
In an assert, assert-on-arrays then advises np.testing.assert_array_equal(a, b), which shows the elements that differ.
Diagnostic message
Section titled “Diagnostic message”This compares two single booleans, not the arrays, so it is True for many arrays that differ. Write np.array_equal($A, $B).See all rules in the NumPy pack.