diff options
author | cpopa <devnull@localhost> | 2014-08-27 15:54:36 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2014-08-27 15:54:36 +0300 |
commit | 2995e293950edc39b0466d1274affc1136c066c8 (patch) | |
tree | 357c129b264bcbb44ce36e1ae8470b198903c239 | |
parent | 476396bf421bea3556645b5b0795011daddeb9bf (diff) | |
download | pylint-2995e293950edc39b0466d1274affc1136c066c8.tar.gz |
Fix a false positive with 'too-many-format-args', when the format string contains mixed attribute access arguments and manual fields. Closes issue #322.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | checkers/strings.py | 10 | ||||
-rw-r--r-- | test/functional/string_formatting.py | 8 | ||||
-rw-r--r-- | test/functional/string_formatting.txt | 2 |
4 files changed, 16 insertions, 8 deletions
@@ -88,6 +88,10 @@ ChangeLog for Pylint * Generate html output for missing files. Closes issue #320. + * Fix a false positive with 'too-many-format-args', when the format + string contains mixed attribute access arguments and manual + fields. Closes issue #322. + 2014-07-26 -- 1.3.0 diff --git a/checkers/strings.py b/checkers/strings.py index a30d29c..40995f6 100644 --- a/checkers/strings.py +++ b/checkers/strings.py @@ -178,6 +178,7 @@ def parse_format_method_string(format_string): if isinstance(keyname, numbers.Number): # In Python 2 it will return long which will lead # to different output between 2 and 3 + manual_pos_arg.add(keyname) keyname = int(keyname) keys.append((keyname, list(fielditerator))) else: @@ -363,8 +364,6 @@ class StringMethodsChecker(BaseChecker): self.add_message('bad-format-string', node=node) return - manual_fields = set(field[0] for field in fields - if isinstance(field[0], numbers.Number)) named_fields = set(field[0] for field in fields if isinstance(field[0], basestring)) if num_args and manual_pos: @@ -405,12 +404,7 @@ class StringMethodsChecker(BaseChecker): # num_args can be 0 if manual_pos is not. num_args = num_args or manual_pos if positional > num_args: - # We can have two possibilities: - # * "{0} {1}".format(a, b) - # * "{} {} {}".format(a, b, c, d) - # We can check the manual keys for the first one. - if len(manual_fields) != positional: - self.add_message('too-many-format-args', node=node) + self.add_message('too-many-format-args', node=node) elif positional < num_args: self.add_message('too-few-format-args', node=node) diff --git a/test/functional/string_formatting.py b/test/functional/string_formatting.py index 594c870..8d9707c 100644 --- a/test/functional/string_formatting.py +++ b/test/functional/string_formatting.py @@ -119,3 +119,11 @@ 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]
+
+def issue322():
+ """ Test a regression using mixed manual position arguments
+ and attribute access arguments.
+ """
+ '{0}{1[FOO]}'.format(123, {'FOO': 456})
+ '{0}{1[FOO]}'.format(123, {'FOO': 456}, 321) # [too-many-format-args]
+ '{0}{1[FOO]}'.format(123) # [too-few-format-args]
diff --git a/test/functional/string_formatting.txt b/test/functional/string_formatting.txt index 593c161..5a4bf66 100644 --- a/test/functional/string_formatting.txt +++ b/test/functional/string_formatting.txt @@ -34,3 +34,5 @@ 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 +too-many-format-args:128:issue322:Too many arguments for format string +too-few-format-args:129:issue322:Not enough arguments for format string |