Commit 8ed6d5fa by Ned Batchelder Committed by GitHub

Merge pull request #44 from edx/ned/fix-43

Stop false warnings on chained comparisons. Fixes #43
parents f3b05740 2c32ba89
......@@ -18,8 +18,8 @@ def register_checkers(linter):
class AssertChecker(BaseChecker):
"""
Implements a few pylint checks on unitests asserts - making sure the right assert is used if assertTrue or
assertFalse are misused.
Implements a few pylint checks on unitests asserts - making sure the right
assert is used if assertTrue or assertFalse are misused.
"""
__implements__ = (IAstroidChecker,)
......@@ -77,7 +77,8 @@ class AssertChecker(BaseChecker):
Check that various assertTrue/False functions are not misused.
"""
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
if node.func.attrname not in self.AFFECTED_ASSERTS:
......@@ -88,6 +89,10 @@ class AssertChecker(BaseChecker):
existing_code = "%s(%s)" % (node.func.attrname, first_arg.as_string())
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]
right = first_arg.ops[0][1]
if isinstance(right, astroid.Const) and right.value is None:
......
......@@ -56,3 +56,13 @@ class TestStringMethods(unittest.TestCase):
This uses the wrong assert, but has a pragma to quiet the message.
"""
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