Sonar: Replace == comparison with numpy.isnan()
sonar:python/numpy-nan-equality-S6725​
Importance | Review Guidance | Requires Scanning Tool |
---|---|---|
Low | Merge Without Review | Yes (Sonar) |
This codemod acts upon the following Sonar rules: python:S6725.
Comparisons against numpy.nan
always result in False
. Thus comparing an expression directly against numpy.nan
is always unintended. The correct way to compare a value for NaN
is to use the numpy.isnan
function.
Our changes look something like this:
import numpy as np
a = np.nan
-if a == np.nan:
+if np.isnan(a):
pass
If you have feedback on this codemod, please let us know!
F.A.Q.​
Why is this codemod marked as Merge Without Review?​
We believe any use of ==
to compare with numpy.nan
is unintended given that it is always False
. Thus we consider this change safe.
Codemod Settings​
N/A