summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-09-18 23:32:20 +0000
committerClaudiu Popa <pcmanticore@gmail.com>2015-09-18 23:32:20 +0000
commit202f9fafec163813170ff37d42fb52796d5affe5 (patch)
tree8ae6e6adb23e107d450eed879205c469bb1ae4a6
parent2888519276d620fc4fd6f6fc57451ac8403bbdfe (diff)
downloadpylint-202f9fafec163813170ff37d42fb52796d5affe5.tar.gz
Exempt str.format from redundant-keyword-arg when self is passed as a keyword
It's perfectly valid to pass self as a keyword to str.format when accessing attributes using format's dot syntax, as in the following example: def __str__(self): return "{self.value}{self.other_value}...".format(self=self) We're exempting only str.format with this commit, because for any other function or method it's either impossible to do so or it's weird and a potential sign of misdesign. Closes issue #642.
-rw-r--r--pylint/checkers/typecheck.py12
-rw-r--r--pylint/test/functional/arguments.py8
2 files changed, 18 insertions, 2 deletions
diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py
index fb1c5fb..b971503 100644
--- a/pylint/checkers/typecheck.py
+++ b/pylint/checkers/typecheck.py
@@ -42,6 +42,7 @@ _ZOPE_DEPRECATED = (
"This option is deprecated. Use generated-members instead."
)
BUILTINS = six.moves.builtins.__name__
+STR_FORMAT = "%s.str.format" % BUILTINS
def _unflatten(iterable):
@@ -537,8 +538,15 @@ accessed. Python regular expressions are accepted.'}
i = parameter_name_to_index[keyword]
if parameters[i][1]:
# Duplicate definition of function parameter.
- self.add_message('redundant-keyword-arg',
- node=node, args=(keyword, callable_name))
+
+ # Might be too hardcoded, but this can actually
+ # happen when using str.format and `self` is passed
+ # by keyword argument, as in `.format(self=self)`.
+ # It's perfectly valid to so, so we're just skipping
+ # it if that's the case.
+ if not (keyword == 'self' and called.qname() == STR_FORMAT):
+ self.add_message('redundant-keyword-arg',
+ node=node, args=(keyword, callable_name))
else:
parameters[i][1] = True
elif keyword in kwparams:
diff --git a/pylint/test/functional/arguments.py b/pylint/test/functional/arguments.py
index a589af4..f8db22c 100644
--- a/pylint/test/functional/arguments.py
+++ b/pylint/test/functional/arguments.py
@@ -134,3 +134,11 @@ class Test(object):
self.lam(1, 2, 3) # [too-many-function-args]
Test().lam() # [no-value-for-parameter]
+
+# Don't emit a redundant-keyword-arg for this example,
+# it's perfectly valid
+
+class Issue642(object):
+ attr = 0
+ def __str__(self):
+ return "{self.attr}".format(self=self)