numpy/mask-operator-precedence
| Rule | mask-operator-precedence |
|---|---|
| Pack | NumPy |
| Severity | error |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Python applies & and | before comparisons, so x > 2 & x < 8 compares against the wrong value. Put each comparison in parentheses.
inside = x[x > 2 & x < 8]inside = x[(x > 2) & (x < 8)]What the rule reports
Section titled “What the rule reports”The rule reports x > 2 & x < 8. Python applies & before the comparisons, so the code compares x with 2 & x.
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 (x > 2) & (x < 8) when the four operands are names, numbers or attributes. Other operands have no fix.
In a file that imports pandas, the pandas rule mask-precedence reports the code instead.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
The & and | operators bind tighter than comparisons, so this compares against a combined value. Write $FIXED.Browse the other rules of the NumPy pack.