From da03d60582b624729f578f0b6dfa0c2c08184c7f Mon Sep 17 00:00:00 2001 From: cpopa Date: Tue, 29 Jul 2014 14:56:19 +0300 Subject: Fix a false positive with string formatting checker, when using keyword argument packing. Closes issue #288. --- ChangeLog | 3 +++ checkers/strings.py | 16 +++++++++++----- test/input/func_string_format_py27.py | 8 ++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4bef070..b5bea9a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,9 @@ ChangeLog for Pylint encountering a string which uses only position-based arguments. Closes issue #285. + * Fix a false positive with string formatting checker, when using + keyword argument packing. Closes issue #288. + 2014-07-26 -- 1.3.0 * Allow hanging continued indentation for implicitly concatenated diff --git a/checkers/strings.py b/checkers/strings.py index a07482a..6f6f3bb 100644 --- a/checkers/strings.py +++ b/checkers/strings.py @@ -386,24 +386,30 @@ class StringMethodsChecker(BaseChecker): key = 0 if isinstance(key, int): try: - argument = utils.get_argument_from_call(node, key) + argname = utils.get_argument_from_call(node, key) except utils.NoSuchArgumentError: continue else: if key not in named: continue - argument = named[key] - if argument in (astroid.YES, None): + argname = named[key] + if argname in (astroid.YES, None): continue try: - argument = argument.infer().next() + argument = argname.infer().next() except astroid.InferenceError: continue if not specifiers or argument is astroid.YES: # No need to check this key if it doesn't # use attribute / item access continue - + if argument.parent and isinstance(argument.parent, astroid.Arguments): + # Check to see if our argument is kwarg or vararg, + # and skip the check for this argument if so, because when inferring, + # astroid will return empty objects (dicts and tuples) and + # that can lead to false positives. + if argname.name in (argument.parent.kwarg, argument.parent.vararg): + continue previous = argument parsed = [] for is_attribute, specifier in specifiers: diff --git a/test/input/func_string_format_py27.py b/test/input/func_string_format_py27.py index 38eafd1..e2bfe12 100644 --- a/test/input/func_string_format_py27.py +++ b/test/input/func_string_format_py27.py @@ -78,3 +78,11 @@ def pprint_bad(): print "{0.missing}".format(2) print "{0} {1} {2}".format(1, 2) print "{0} {1}".format(1, 2, 3) + +def good_issue288(*args, **kwargs): + """ Test that using kwargs does not emit a false + positive. + """ + data = 'Hello John Doe {0[0]}'.format(args) + print data + return 'Hello {0[name]}'.format(kwargs) -- cgit v1.2.1