From 723207b6a03e35cab88d6014125808592fce9176 Mon Sep 17 00:00:00 2001 From: cpopa Date: Mon, 11 Aug 2014 16:43:14 +0300 Subject: Fix a false positive with 'too-few-format-args', when the format strings contains duplicate manual position arguments. Closes issue #310. --- ChangeLog | 4 ++++ checkers/strings.py | 6 +++--- test/functional/string_formatting.py | 5 +++++ test/functional/string_formatting.txt | 1 + 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 -- cgit v1.2.1