summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcpopa <devnull@localhost>2014-07-25 17:13:50 +0200
committercpopa <devnull@localhost>2014-07-25 17:13:50 +0200
commitf99a41b44da0fc78682081393fdda3644ee7b799 (patch)
tree113ecc28a525e909b0906cb439d65c3a5e814b71
parent32db43b53f4670b32eb2e140b963f94fddb58eea (diff)
downloadpylint-f99a41b44da0fc78682081393fdda3644ee7b799.tar.gz
Don't emit 'unnecessary-lambda' if the body of the lambda call contains call chaining. Closes issue #243.
-rw-r--r--ChangeLog11
-rw-r--r--checkers/base.py5
-rw-r--r--test/functional/unnecessary_lambda.py (renamed from test/input/func_w0108.py)90
-rw-r--r--test/functional/unnecessary_lambda.txt7
-rw-r--r--test/messages/func_w0108.txt8
5 files changed, 69 insertions, 52 deletions
diff --git a/ChangeLog b/ChangeLog
index 33a784f..4f91ebf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -58,11 +58,14 @@ ChangeLog for Pylint
* Use the proper mode for pickle when opening and writing the stats file.
Closes issue #148.
- * Don't emit hidden-method message when the attribute has been
- monkey-patched, you're on your own when you do that
+ * Don't emit hidden-method message when the attribute has been
+ monkey-patched, you're on your own when you do that.
- * Only emit attribute-defined-outside-init for definition within the same
- module as the offended class, avoiding to mangle the output in some cases
+ * Only emit attribute-defined-outside-init for definition within the same
+ module as the offended class, avoiding to mangle the output in some cases.
+
+ * Don't emit 'unnecessary-lambda' if the body of the lambda call contains
+ call chaining. Closes issue #243.
2014-04-30 -- 1.2.1
diff --git a/checkers/base.py b/checkers/base.py
index 63b153b..1a41f59 100644
--- a/checkers/base.py
+++ b/checkers/base.py
@@ -630,6 +630,11 @@ functions, methods
return
if node.args.args[i].name != call.args[i].name:
return
+ if (isinstance(node.body.func, astroid.Getattr) and
+ isinstance(node.body.func.expr, astroid.CallFunc)):
+ # Chained call, the intermediate call might
+ # return something else (but we don't check that, yet).
+ return
self.add_message('unnecessary-lambda', line=node.fromlineno, node=node)
@check_messages('dangerous-default-value')
diff --git a/test/input/func_w0108.py b/test/functional/unnecessary_lambda.py
index 0d4cc62..1c9b424 100644
--- a/test/input/func_w0108.py
+++ b/test/functional/unnecessary_lambda.py
@@ -1,40 +1,50 @@
-# pylint: disable=W0142
-"""test suspicious lambda expressions
-"""
-
-__revision__ = ''
-
-# Some simple examples of the most commonly encountered forms.
-_ = lambda: list() # replaceable with "list"
-_ = lambda x: hash(x) # replaceable with "hash"
-_ = lambda x, y: min(x, y) # replaceable with "min"
-
-# A function that can take any arguments given to it.
-_ANYARGS = lambda *args, **kwargs: 'completely arbitrary return value'
-
-# Some more complex forms of unnecessary lambda expressions.
-_ = lambda *args: _ANYARGS(*args)
-_ = lambda **kwargs: _ANYARGS(**kwargs)
-_ = lambda *args, **kwargs: _ANYARGS(*args, **kwargs)
-_ = lambda x, y, z, *args, **kwargs: _ANYARGS(x, y, z, *args, **kwargs)
-
-# Lambdas that are *not* unnecessary and should *not* trigger warnings.
-_ = lambda x: x
-_ = lambda x: x()
-_ = lambda x=4: hash(x)
-_ = lambda x, y: range(y, x)
-_ = lambda x: range(5, x)
-_ = lambda x, y: range(x, 5)
-_ = lambda x, y, z: x.y(z)
-_ = lambda: 5
-_ = lambda **kwargs: _ANYARGS()
-_ = lambda **kwargs: _ANYARGS(**dict([('three', 3)]))
-_ = lambda **kwargs: _ANYARGS(**{'three': 3})
-_ = lambda dict_arg, **kwargs: _ANYARGS(kwargs, **dict_arg)
-_ = lambda *args: _ANYARGS()
-_ = lambda *args: _ANYARGS(*list([3, 4]))
-_ = lambda *args: _ANYARGS(*[3, 4])
-_ = lambda list_arg, *args: _ANYARGS(args, *list_arg)
-_ = lambda: _ANYARGS(*[3])
-_ = lambda: _ANYARGS(**{'three': 3})
-_ = lambda: _ANYARGS(*[3], **{'three': 3})
+# pylint: disable=star-args, undefined-variable
+"""test suspicious lambda expressions
+"""
+
+__revision__ = ''
+
+# Some simple examples of the most commonly encountered forms.
+# +1: [unnecessary-lambda]
+_ = lambda: list() # replaceable with "list"
+# +1: [unnecessary-lambda]
+_ = lambda x: hash(x) # replaceable with "hash"
+# +1: [unnecessary-lambda]
+_ = lambda x, y: min(x, y) # replaceable with "min"
+
+# A function that can take any arguments given to it.
+_ANYARGS = lambda *args, **kwargs: 'completely arbitrary return value'
+
+# Some more complex forms of unnecessary lambda expressions.
+# +1: [unnecessary-lambda]
+_ = lambda *args: _ANYARGS(*args)
+# +1: [unnecessary-lambda]
+_ = lambda **kwargs: _ANYARGS(**kwargs)
+# +1: [unnecessary-lambda]
+_ = lambda *args, **kwargs: _ANYARGS(*args, **kwargs)
+# +1: [unnecessary-lambda]
+_ = lambda x, y, z, *args, **kwargs: _ANYARGS(x, y, z, *args, **kwargs)
+
+# Lambdas that are *not* unnecessary and should *not* trigger warnings.
+_ = lambda x: x
+_ = lambda x: x()
+_ = lambda x=4: hash(x)
+_ = lambda x, y: list(range(y, x))
+_ = lambda x: list(range(5, x))
+_ = lambda x, y: list(range(x, 5))
+_ = lambda x, y, z: x.y(z)
+_ = lambda: 5
+_ = lambda **kwargs: _ANYARGS()
+_ = lambda **kwargs: _ANYARGS(**dict([('three', 3)]))
+_ = lambda **kwargs: _ANYARGS(**{'three': 3})
+_ = lambda dict_arg, **kwargs: _ANYARGS(kwargs, **dict_arg)
+_ = lambda *args: _ANYARGS()
+_ = lambda *args: _ANYARGS(*list([3, 4]))
+_ = lambda *args: _ANYARGS(*[3, 4])
+_ = lambda list_arg, *args: _ANYARGS(args, *list_arg)
+_ = lambda: _ANYARGS(*[3])
+_ = lambda: _ANYARGS(**{'three': 3})
+_ = lambda: _ANYARGS(*[3], **{'three': 3})
+
+# Don't warn about this.
+_ = lambda: code().analysis()
diff --git a/test/functional/unnecessary_lambda.txt b/test/functional/unnecessary_lambda.txt
new file mode 100644
index 0000000..de13882
--- /dev/null
+++ b/test/functional/unnecessary_lambda.txt
@@ -0,0 +1,7 @@
+unnecessary-lambda:9:<lambda>:Lambda may not be necessary
+unnecessary-lambda:11:<lambda>:Lambda may not be necessary
+unnecessary-lambda:13:<lambda>:Lambda may not be necessary
+unnecessary-lambda:20:<lambda>:Lambda may not be necessary
+unnecessary-lambda:22:<lambda>:Lambda may not be necessary
+unnecessary-lambda:24:<lambda>:Lambda may not be necessary
+unnecessary-lambda:26:<lambda>:Lambda may not be necessary
diff --git a/test/messages/func_w0108.txt b/test/messages/func_w0108.txt
deleted file mode 100644
index 70e0925..0000000
--- a/test/messages/func_w0108.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-W: 8:<lambda>: Lambda may not be necessary
-W: 9:<lambda>: Lambda may not be necessary
-W: 10:<lambda>: Lambda may not be necessary
-W: 16:<lambda>: Lambda may not be necessary
-W: 17:<lambda>: Lambda may not be necessary
-W: 18:<lambda>: Lambda may not be necessary
-W: 19:<lambda>: Lambda may not be necessary
-