diff options
Diffstat (limited to 'pylint/test')
-rw-r--r-- | pylint/test/functional/comparisons.py | 11 | ||||
-rw-r--r-- | pylint/test/functional/comparisons.txt | 5 | ||||
-rw-r--r-- | pylint/test/unittest_checker_base.py | 46 |
3 files changed, 62 insertions, 0 deletions
diff --git a/pylint/test/functional/comparisons.py b/pylint/test/functional/comparisons.py new file mode 100644 index 0000000..73067c8 --- /dev/null +++ b/pylint/test/functional/comparisons.py @@ -0,0 +1,11 @@ +# pylint: disable=missing-docstring, invalid-name +x = 42 +a = x is None +b = x == None # [singleton-comparison] +c = x == True # [singleton-comparison] +d = x == False # [singleton-comparison] +e = True == True # [singleton-comparison] +f = x is 1 +g = 123 is "123" +h = None is x +i = None == x # [singleton-comparison] diff --git a/pylint/test/functional/comparisons.txt b/pylint/test/functional/comparisons.txt new file mode 100644 index 0000000..fd1fa98 --- /dev/null +++ b/pylint/test/functional/comparisons.txt @@ -0,0 +1,5 @@ +singleton-comparison:4::Comparison to None should be 'expr is None' +singleton-comparison:5::Comparison to True should be just 'expr' or 'expr is True' +singleton-comparison:6::Comparison to False should be 'not expr' or 'expr is False' +singleton-comparison:7::Comparison to True should be just 'expr' or 'expr is True' +singleton-comparison:11::Comparison to None should be 'expr is None' diff --git a/pylint/test/unittest_checker_base.py b/pylint/test/unittest_checker_base.py index f19544f..c68f379 100644 --- a/pylint/test/unittest_checker_base.py +++ b/pylint/test/unittest_checker_base.py @@ -246,6 +246,52 @@ class MultiNamingStyleTest(CheckerTestCase): self.checker.visit_functiondef(func) self.checker.leave_module(func.root) +class ComparisonTest(CheckerTestCase): + CHECKER_CLASS = base.ComparisonChecker + + def test_singleton_comparison(self): + node = test_utils.extract_node("foo == True") + message = Message('singleton-comparison', + node=node, + args=(True, "just 'expr' or 'expr is True'")) + with self.assertAddsMessages(message): + self.checker.visit_compare(node) + + node = test_utils.extract_node("foo == False") + message = Message('singleton-comparison', + node=node, + args=(False, "'not expr' or 'expr is False'")) + with self.assertAddsMessages(message): + self.checker.visit_compare(node) + + node = test_utils.extract_node("foo == None") + message = Message('singleton-comparison', + node=node, + args=(None, "'expr is None'")) + with self.assertAddsMessages(message): + self.checker.visit_compare(node) + + node = test_utils.extract_node("True == foo") + message = Message('singleton-comparison', + node=node, + args=(True, "just 'expr' or 'expr is True'")) + with self.assertAddsMessages(message): + self.checker.visit_compare(node) + + node = test_utils.extract_node("False == foo") + message = Message('singleton-comparison', + node=node, + args=(False, "'not expr' or 'expr is False'")) + with self.assertAddsMessages(message): + self.checker.visit_compare(node) + + node = test_utils.extract_node("None == foo") + message = Message('singleton-comparison', + node=node, + args=(None, "'expr is None'")) + with self.assertAddsMessages(message): + self.checker.visit_compare(node) + if __name__ == '__main__': unittest.main() |