Skip to main content

Sonar: Replace == comparison with numpy.isnan()


ImportanceReview GuidanceRequires Scanning Tool
MediumMerge Without ReviewYes (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):

If you have feedback on this codemod, please let us know!


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