From 3e9579375633feb04e210ef8e49c61e8a29fd78e Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 5 Jun 2017 00:22:26 +0200 Subject: [PATCH] Fix the lambda issues. --- parso/python/normalizer.py | 5 ++++- test/normalizer_issue_files/E73.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/normalizer_issue_files/E73.py diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 3d43b7f..3965d23 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -47,7 +47,10 @@ class PEP8Normalizer(Normalizer): for name in names[:1]: self.add_issue(401, 'Multiple imports on one line', name) elif typ == 'lambdef': - if node.parent.type == 'expr_stmt': + expr_stmt = node.parent + # Check if it's simply defining a single name, not something like + # foo.bar or x[1], where using a lambda could make more sense. + if expr_stmt.type == 'expr_stmt' and any(n.type == 'name' for n in expr_stmt.children[:-2:2]): self.add_issue(731, 'Do not assign a lambda expression, use a def', node) elif typ == 'try_stmt': for child in node.children: diff --git a/test/normalizer_issue_files/E73.py b/test/normalizer_issue_files/E73.py new file mode 100644 index 0000000..b79e4f6 --- /dev/null +++ b/test/normalizer_issue_files/E73.py @@ -0,0 +1,16 @@ +#: E731:4 +f = lambda x: 2 * x +while False: + #: E731:6 + foo = lambda y, z: 2 * x +# Okay +f = object() +f.method = lambda: 'Method' + +f = {} +f['a'] = lambda x: x ** 2 + +f = [] +f.append(lambda x: x ** 2) + +lambda: 'no-op'