diff options
author | John Vandenberg <jayvdb@gmail.com> | 2015-11-24 23:41:41 +1100 |
---|---|---|
committer | John Vandenberg <jayvdb@gmail.com> | 2015-11-24 23:41:41 +1100 |
commit | 4e264a1f227f5a1dc664a3712dace4301c29e0dc (patch) | |
tree | 5dca9c91c48bb6c28608fdfef708dcee2532cfdd | |
parent | 265766da8921620b44752bdc6ba95b3775f0390b (diff) | |
parent | c3255c73629b282be3dccbc7887f608d2359593b (diff) | |
download | pyflakes-4e264a1f227f5a1dc664a3712dace4301c29e0dc.tar.gz |
Merge pull request #51 from jayvdb/assert-tuple
Report assert using tuple
-rw-r--r-- | pyflakes/checker.py | 7 | ||||
-rw-r--r-- | pyflakes/messages.py | 7 | ||||
-rw-r--r-- | pyflakes/test/test_other.py | 34 |
3 files changed, 47 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index ddaf86e..58b3826 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -671,7 +671,7 @@ class Checker(object): # "stmt" type nodes DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \ - ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = ASSERT = EXEC = \ + ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = EXEC = \ EXPR = ASSIGN = handleChildren PASS = ignore @@ -697,6 +697,11 @@ class Checker(object): # additional node types COMPREHENSION = KEYWORD = FORMATTEDVALUE = handleChildren + def ASSERT(self, node): + if isinstance(node.test, ast.Tuple) and node.test.elts != []: + self.report(messages.AssertTuple, node) + self.handleChildren(node) + def GLOBAL(self, node): """ Keep track of globals declarations. diff --git a/pyflakes/messages.py b/pyflakes/messages.py index 40142a4..9380d02 100644 --- a/pyflakes/messages.py +++ b/pyflakes/messages.py @@ -191,3 +191,10 @@ class TooManyExpressionsInStarredAssignment(Message): Too many expressions in an assignment with star-unpacking """ message = 'too many expressions in star-unpacking assignment' + + +class AssertTuple(Message): + """ + Assertion test is a tuple, which are always True. + """ + message = 'assertion is always true, perhaps remove parentheses?' diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index 1891c15..0ac96a3 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -1633,6 +1633,40 @@ class TestUnusedAssignment(TestCase): baz += bar() ''') + def test_assert_without_message(self): + """An assert without a message is not an error.""" + self.flakes(''' + a = 1 + assert a + ''') + + def test_assert_with_message(self): + """An assert with a message is not an error.""" + self.flakes(''' + a = 1 + assert a, 'x' + ''') + + def test_assert_tuple(self): + """An assert of a non-empty tuple is always True.""" + self.flakes(''' + assert (False, 'x') + assert (False, ) + ''', m.AssertTuple, m.AssertTuple) + + def test_assert_tuple_empty(self): + """An assert of an empty tuple is always False.""" + self.flakes(''' + assert () + ''') + + def test_assert_static(self): + """An assert of a static value is not an error.""" + self.flakes(''' + assert True + assert 1 + ''') + @skipIf(version_info < (3, 3), 'new in Python 3.3') def test_yieldFromUndefined(self): """ |