summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOr Bahari <orbahari@mail.tau.ac.il>2021-02-02 01:44:46 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-02-18 15:22:07 +0100
commitf9b2227cb036349dda36101d904496e2b54167e8 (patch)
treee24a3871bdb2ea412f16fa3010a99be3468d7cab /tests
parent4faccd0563c628954f04322ffb696905a3dab62d (diff)
downloadpylint-git-f9b2227cb036349dda36101d904496e2b54167e8.tar.gz
add nan-comparison checker for NaN comparisons
**After fix whatsnew to 2.7
Diffstat (limited to 'tests')
-rw-r--r--tests/checkers/unittest_base.py37
-rw-r--r--tests/functional/n/nan_comparison_check.py21
-rw-r--r--tests/functional/n/nan_comparison_check.txt12
3 files changed, 70 insertions, 0 deletions
diff --git a/tests/checkers/unittest_base.py b/tests/checkers/unittest_base.py
index a106e8c2b..3c2e443dc 100644
--- a/tests/checkers/unittest_base.py
+++ b/tests/checkers/unittest_base.py
@@ -445,6 +445,43 @@ class TestComparison(CheckerTestCase):
with self.assertAddsMessages(message):
self.checker.visit_compare(node)
+ node = astroid.extract_node("foo is float('nan')")
+ message = Message(
+ "nan-comparison",
+ node=node,
+ args=("'foo is float('nan')'", "'math.isnan(foo)'"),
+ )
+ with self.assertAddsMessages(message):
+ self.checker.visit_compare(node)
+
+ node = astroid.extract_node(
+ """
+ import numpy
+ foo != numpy.NaN
+ """
+ )
+ message = Message(
+ "nan-comparison",
+ node=node,
+ args=("'foo != numpy.NaN'", "'not math.isnan(foo)'"),
+ )
+ with self.assertAddsMessages(message):
+ self.checker.visit_compare(node)
+
+ node = astroid.extract_node(
+ """
+ import numpy as nmp
+ foo is not nmp.NaN
+ """
+ )
+ message = Message(
+ "nan-comparison",
+ node=node,
+ args=("'foo is not nmp.NaN'", "'not math.isnan(foo)'"),
+ )
+ with self.assertAddsMessages(message):
+ self.checker.visit_compare(node)
+
node = astroid.extract_node("True == foo")
messages = (
Message("misplaced-comparison-constant", node=node, args=("foo == True",)),
diff --git a/tests/functional/n/nan_comparison_check.py b/tests/functional/n/nan_comparison_check.py
new file mode 100644
index 000000000..db539a11e
--- /dev/null
+++ b/tests/functional/n/nan_comparison_check.py
@@ -0,0 +1,21 @@
+# pylint: disable=missing-docstring, invalid-name, misplaced-comparison-constant,literal-comparison,comparison-with-itself, import-error
+"""Test detection of NaN value comparison."""
+
+import numpy
+x = 42
+a = x is numpy.NaN # [nan-comparison]
+b = x == numpy.NaN # [nan-comparison]
+c = x == float('nan') # [nan-comparison]
+e = numpy.NaN == numpy.NaN # [nan-comparison]
+f = x is 1
+g = 123 is "123"
+h = numpy.NaN is not x # [nan-comparison]
+i = numpy.NaN != x # [nan-comparison]
+
+j = x != numpy.NaN # [nan-comparison]
+j1 = x != float('nan') # [nan-comparison]
+assert x == numpy.NaN # [nan-comparison]
+assert x is not float('nan') # [nan-comparison]
+if x == numpy.NaN: # [nan-comparison]
+ pass
+z = bool(x is numpy.NaN) # [nan-comparison]
diff --git a/tests/functional/n/nan_comparison_check.txt b/tests/functional/n/nan_comparison_check.txt
new file mode 100644
index 000000000..2351ea403
--- /dev/null
+++ b/tests/functional/n/nan_comparison_check.txt
@@ -0,0 +1,12 @@
+nan-comparison:6:4::Comparison 'x is numpy.NaN' should be 'math.isnan(x)'
+nan-comparison:7:4::Comparison 'x == numpy.NaN' should be 'math.isnan(x)'
+nan-comparison:8:4::Comparison 'x == float('nan')' should be 'math.isnan(x)'
+nan-comparison:9:4::Comparison 'numpy.NaN == numpy.NaN' should be 'math.isnan(numpy.NaN)'
+nan-comparison:12:4::Comparison 'numpy.NaN is not x' should be 'not math.isnan(x)'
+nan-comparison:13:4::Comparison 'numpy.NaN != x' should be 'not math.isnan(x)'
+nan-comparison:15:4::Comparison 'x != numpy.NaN' should be 'not math.isnan(x)'
+nan-comparison:16:5::Comparison 'x != float('nan')' should be 'not math.isnan(x)'
+nan-comparison:17:7::Comparison 'x == numpy.NaN' should be 'math.isnan(x)'
+nan-comparison:18:7::Comparison 'x is not float('nan')' should be 'not math.isnan(x)'
+nan-comparison:19:3::Comparison 'x == numpy.NaN' should be 'math.isnan(x)'
+nan-comparison:21:9::Comparison 'x is numpy.NaN' should be 'math.isnan(x)'