Fix the lambda issues.

This commit is contained in:
Dave Halter
2017-06-05 00:22:26 +02:00
parent d1d02ba3f5
commit 3e95793756
2 changed files with 20 additions and 1 deletions

View File

@@ -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:

View File

@@ -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'