Skip to content

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)]

The rule reports x > 2 & x < 8. Python applies & before the comparisons, so the code compares x with 2 & x.

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.

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.