Skip to content

Commit db145a2

Browse files
committed
Merge pull request #87 from twisted/percent-dict
don't be overly zealous about checking the right hand of % for tuples
2 parents 4af4e97 + 3ab4dda commit db145a2

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

twistedchecker/checkers/formattingoperation.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ def visit_binop(self, node):
2727
if node.op != "%":
2828
return
2929
pattern = node.left.as_string()
30+
# If the pattern's not a constant string, we don't know whether a
31+
# dictionary or a tuple makes sense, so don't try to guess.
32+
if not pattern.startswith("'") or pattern.startswith('"'):
33+
return
34+
# If the pattern has things like %(foo)s, then the values can't be a
35+
# tuple, so don't check for it.
36+
if "%(" in pattern:
37+
return
3038
valueString = node.right.as_string()
31-
# If the pattern has things like %(foo)s,
32-
# then the values can't be a tuple, so don't check for it.
33-
if "%(" not in pattern:
34-
tupleUsed = valueString.startswith('(')
35-
if not tupleUsed:
36-
self.add_message('W9501', node=node)
39+
tupleUsed = valueString.startswith('(')
40+
if tupleUsed:
41+
return
42+
self.add_message('W9501', node=node)

twistedchecker/functionaltests/formattingoperation.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# enable: W9501
2-
2+
# -*- test-case-name: twistedchecker.test.test_functionaltests -*-
33

44
num = 3
55
# we should use a tuple as the value
@@ -15,3 +15,8 @@
1515
num = 3
1616
# a tuple used in the string formatting operation.
1717
formattedString = "%d" % (num,)
18+
19+
# a formatting operation using mapping value
20+
# no warnings should be generated
21+
constantFormat = "a format %(value)s"
22+
constantFormat % {"value": "value"}

0 commit comments

Comments
 (0)