Skip to content

Commit c3255c7

Browse files
committed
Report assert using tuple
This is a SyntaxWarning on Python 2.6+ Resolves lp:848467
1 parent 265766d commit c3255c7

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

pyflakes/checker.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def ignore(self, node):
671671

672672
# "stmt" type nodes
673673
DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \
674-
ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = ASSERT = EXEC = \
674+
ASYNCWITH = ASYNCWITHITEM = RAISE = TRYFINALLY = EXEC = \
675675
EXPR = ASSIGN = handleChildren
676676

677677
PASS = ignore
@@ -697,6 +697,11 @@ def ignore(self, node):
697697
# additional node types
698698
COMPREHENSION = KEYWORD = FORMATTEDVALUE = handleChildren
699699

700+
def ASSERT(self, node):
701+
if isinstance(node.test, ast.Tuple) and node.test.elts != []:
702+
self.report(messages.AssertTuple, node)
703+
self.handleChildren(node)
704+
700705
def GLOBAL(self, node):
701706
"""
702707
Keep track of globals declarations.

pyflakes/messages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,10 @@ class TooManyExpressionsInStarredAssignment(Message):
191191
Too many expressions in an assignment with star-unpacking
192192
"""
193193
message = 'too many expressions in star-unpacking assignment'
194+
195+
196+
class AssertTuple(Message):
197+
"""
198+
Assertion test is a tuple, which are always True.
199+
"""
200+
message = 'assertion is always true, perhaps remove parentheses?'

pyflakes/test/test_other.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,40 @@ def test_augmentedAssignmentImportedFunctionCall(self):
16331633
baz += bar()
16341634
''')
16351635

1636+
def test_assert_without_message(self):
1637+
"""An assert without a message is not an error."""
1638+
self.flakes('''
1639+
a = 1
1640+
assert a
1641+
''')
1642+
1643+
def test_assert_with_message(self):
1644+
"""An assert with a message is not an error."""
1645+
self.flakes('''
1646+
a = 1
1647+
assert a, 'x'
1648+
''')
1649+
1650+
def test_assert_tuple(self):
1651+
"""An assert of a non-empty tuple is always True."""
1652+
self.flakes('''
1653+
assert (False, 'x')
1654+
assert (False, )
1655+
''', m.AssertTuple, m.AssertTuple)
1656+
1657+
def test_assert_tuple_empty(self):
1658+
"""An assert of an empty tuple is always False."""
1659+
self.flakes('''
1660+
assert ()
1661+
''')
1662+
1663+
def test_assert_static(self):
1664+
"""An assert of a static value is not an error."""
1665+
self.flakes('''
1666+
assert True
1667+
assert 1
1668+
''')
1669+
16361670
@skipIf(version_info < (3, 3), 'new in Python 3.3')
16371671
def test_yieldFromUndefined(self):
16381672
"""

0 commit comments

Comments
 (0)