summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcpopa <devnull@localhost>2014-08-11 16:43:14 +0300
committercpopa <devnull@localhost>2014-08-11 16:43:14 +0300
commit723207b6a03e35cab88d6014125808592fce9176 (patch)
tree350ecc64168f226413cd5efe4c93e33bf2333de7
parent679daa55bfc6f4d0f948ebfd54c6e713887da9ae (diff)
downloadpylint-723207b6a03e35cab88d6014125808592fce9176.tar.gz
Fix a false positive with 'too-few-format-args', when the format strings contains duplicate manual position arguments. Closes issue #310.
-rw-r--r--ChangeLog4
-rw-r--r--checkers/strings.py6
-rw-r--r--test/functional/string_formatting.py5
-rw-r--r--test/functional/string_formatting.txt1
4 files changed, 13 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 050bc9a..287005f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -50,6 +50,10 @@ ChangeLog for Pylint
* Don't count branches from nested functions.
+ * Fix a false positive with 'too-few-format-args', when the format
+ strings contains duplicate manual position arguments.
+ Closes issue #310.
+
2014-07-26 -- 1.3.0
* Allow hanging continued indentation for implicitly concatenated
diff --git a/checkers/strings.py b/checkers/strings.py
index 522db4e..a30d29c 100644
--- a/checkers/strings.py
+++ b/checkers/strings.py
@@ -169,10 +169,10 @@ def parse_format_method_string(format_string):
"""
keys = []
num_args = 0
- manual_pos_arg = 0
+ manual_pos_arg = set()
for name in collect_string_fields(format_string):
if name and str(name).isdigit():
- manual_pos_arg += 1
+ manual_pos_arg.add(str(name))
elif name:
keyname, fielditerator = split_format_field_names(name)
if isinstance(keyname, numbers.Number):
@@ -182,7 +182,7 @@ def parse_format_method_string(format_string):
keys.append((keyname, list(fielditerator)))
else:
num_args += 1
- return keys, num_args, manual_pos_arg
+ return keys, num_args, len(manual_pos_arg)
def get_args(callfunc):
""" Get the arguments from the given `CallFunc` node.
diff --git a/test/functional/string_formatting.py b/test/functional/string_formatting.py
index 91b91ba..594c870 100644
--- a/test/functional/string_formatting.py
+++ b/test/functional/string_formatting.py
@@ -114,3 +114,8 @@ def nested_issue294():
'{0:>{1}}'.format(42, 24, 54) # [too-many-format-args]
'{0:{a[1]}}'.format(1) # [missing-format-argument-key]
'{0:{a.x}}'.format(1, a=2) # [missing-format-attribute]
+
+def issue310():
+ """ Test a regression using duplicate manual position arguments. """
+ '{0} {1} {0}'.format(1, 2)
+ '{0} {1} {0}'.format(1) # [too-few-format-args]
diff --git a/test/functional/string_formatting.txt b/test/functional/string_formatting.txt
index ffc00fb..5f27835 100644
--- a/test/functional/string_formatting.txt
+++ b/test/functional/string_formatting.txt
@@ -33,3 +33,4 @@ too-few-format-args:113:nested_issue294:Not enough arguments for format string
too-many-format-args:114:nested_issue294:Too many arguments for format string
missing-format-argument-key:115:nested_issue294:Missing keyword argument 'a' for format string
missing-format-attribute:116:nested_issue294:Missing format attribute 'x' in format specifier 'a.x'
+too-few-format-args:121:issue310:Not enough arguments for format string