Skip to content

Commit 598eb16

Browse files
PeterJCLawbluetech
andauthored
Add a check for if statement conditions which are non-empty tuples (#512)
* Add a check for if statement conditions which are non-empty tuples With the increasing prevalence of wrapping conditionals like: if ( some_value != 'some_condition' and other_value is not 42 ) and for wrapping other constructs with trailing commas, like: x = ( some_value, other_value, ) it becomes quite easy to accidentally combine these into a conditional statement which is actually always true: if ( some_value != 'some_condition' and other_value is not 42, ) This commit introduces a check for such constructs. * Make this error message make sense * Further clarity this message's docstring Co-Authored-By: Ran Benita <[email protected]> Co-authored-by: Ran Benita <[email protected]>
1 parent 38ee670 commit 598eb16

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

pyflakes/checker.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,14 +1409,14 @@ def ignore(self, node):
14091409
pass
14101410

14111411
# "stmt" type nodes
1412-
DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \
1412+
DELETE = PRINT = FOR = ASYNCFOR = WHILE = WITH = WITHITEM = \
14131413
ASYNCWITH = ASYNCWITHITEM = TRYFINALLY = EXEC = \
14141414
EXPR = ASSIGN = handleChildren
14151415

14161416
PASS = ignore
14171417

14181418
# "expr" type nodes
1419-
BOOLOP = UNARYOP = IFEXP = SET = \
1419+
BOOLOP = UNARYOP = SET = \
14201420
REPR = ATTRIBUTE = \
14211421
STARRED = NAMECONSTANT = NAMEDEXPR = handleChildren
14221422

@@ -1773,6 +1773,13 @@ def DICT(self, node):
17731773
)
17741774
self.handleChildren(node)
17751775

1776+
def IF(self, node):
1777+
if isinstance(node.test, ast.Tuple) and node.test.elts != []:
1778+
self.report(messages.IfTuple, node)
1779+
self.handleChildren(node)
1780+
1781+
IFEXP = IF
1782+
17761783
def ASSERT(self, node):
17771784
if isinstance(node.test, ast.Tuple) and node.test.elts != []:
17781785
self.report(messages.AssertTuple, node)

pyflakes/messages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ class TooManyExpressionsInStarredAssignment(Message):
233233
message = 'too many expressions in star-unpacking assignment'
234234

235235

236+
class IfTuple(Message):
237+
"""
238+
Conditional test is a non-empty tuple literal, which are always True.
239+
"""
240+
message = '\'if tuple literal\' is always true, perhaps remove accidental comma?'
241+
242+
236243
class AssertTuple(Message):
237244
"""
238245
Assertion test is a non-empty tuple literal, which are always True.

pyflakes/test/test_other.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,29 @@ def test_ifexp(self):
14331433
self.flakes("a = foo if True else 'oink'", m.UndefinedName)
14341434
self.flakes("a = 'moo' if True else bar", m.UndefinedName)
14351435

1436+
def test_if_tuple(self):
1437+
"""
1438+
Test C{if (foo,)} conditions.
1439+
"""
1440+
self.flakes("""if (): pass""")
1441+
self.flakes("""
1442+
if (
1443+
True
1444+
):
1445+
pass
1446+
""")
1447+
self.flakes("""
1448+
if (
1449+
True,
1450+
):
1451+
pass
1452+
""", m.IfTuple)
1453+
self.flakes("""
1454+
x = 1 if (
1455+
True,
1456+
) else 2
1457+
""", m.IfTuple)
1458+
14361459
def test_withStatementNoNames(self):
14371460
"""
14381461
No warnings are emitted for using inside or after a nameless C{with}

0 commit comments

Comments
 (0)