Commit 2c32ba89 by Ned Batchelder

Stop false warnings on chained comparisons. Fixes #43

parent f3b05740
...@@ -18,8 +18,8 @@ def register_checkers(linter): ...@@ -18,8 +18,8 @@ def register_checkers(linter):
class AssertChecker(BaseChecker): class AssertChecker(BaseChecker):
""" """
Implements a few pylint checks on unitests asserts - making sure the right assert is used if assertTrue or Implements a few pylint checks on unitests asserts - making sure the right
assertFalse are misused. assert is used if assertTrue or assertFalse are misused.
""" """
__implements__ = (IAstroidChecker,) __implements__ = (IAstroidChecker,)
...@@ -77,7 +77,8 @@ class AssertChecker(BaseChecker): ...@@ -77,7 +77,8 @@ class AssertChecker(BaseChecker):
Check that various assertTrue/False functions are not misused. Check that various assertTrue/False functions are not misused.
""" """
if not isinstance(node.func, astroid.Getattr): if not isinstance(node.func, astroid.Getattr):
# If it isn't a getattr ignore this. All the assertMethods are attrs of self: # If it isn't a getattr ignore this. All the assertMethods are
# attributes of self:
return return
if node.func.attrname not in self.AFFECTED_ASSERTS: if node.func.attrname not in self.AFFECTED_ASSERTS:
...@@ -88,6 +89,10 @@ class AssertChecker(BaseChecker): ...@@ -88,6 +89,10 @@ class AssertChecker(BaseChecker):
existing_code = "%s(%s)" % (node.func.attrname, first_arg.as_string()) existing_code = "%s(%s)" % (node.func.attrname, first_arg.as_string())
if isinstance(first_arg, astroid.Compare): if isinstance(first_arg, astroid.Compare):
if len(first_arg.ops) > 1:
# This is a chained comparison, which we can't do anything with.
return
compare = first_arg.ops[0][0] compare = first_arg.ops[0][0]
right = first_arg.ops[0][1] right = first_arg.ops[0][1]
if isinstance(right, astroid.Const) and right.value is None: if isinstance(right, astroid.Const) and right.value is None:
......
...@@ -56,3 +56,13 @@ class TestStringMethods(unittest.TestCase): ...@@ -56,3 +56,13 @@ class TestStringMethods(unittest.TestCase):
This uses the wrong assert, but has a pragma to quiet the message. This uses the wrong assert, but has a pragma to quiet the message.
""" """
self.assertTrue("a" in "lala") # pylint: disable=wrong-assert-type self.assertTrue("a" in "lala") # pylint: disable=wrong-assert-type
def test_chained_comparisons(self):
"""
These uses of assertTrue and assertFalse are fine, because we can't
pick apart the chained comparisons.
"""
my_value = my_other_value = 10
self.assertTrue(0 < my_value < 1000)
self.assertFalse(0 < my_value < 5)
self.assertTrue(my_value == my_other_value == 10)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment