summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Vandenberg <jayvdb@gmail.com>2015-11-21 00:45:27 +1100
committerJohn Vandenberg <jayvdb@gmail.com>2015-11-24 23:38:21 +1100
commitc3255c73629b282be3dccbc7887f608d2359593b (patch)
tree5dca9c91c48bb6c28608fdfef708dcee2532cfdd
parent265766da8921620b44752bdc6ba95b3775f0390b (diff)
downloadpyflakes-c3255c73629b282be3dccbc7887f608d2359593b.tar.gz
Report assert using tuple
This is a SyntaxWarning on Python 2.6+ Resolves lp:848467
-rw-r--r--pyflakes/checker.py7
-rw-r--r--pyflakes/messages.py7
-rw-r--r--pyflakes/test/test_other.py34
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):
"""