PyCQA / pycodestyle

Simple Python style checker in one Python file

Home Page:https://pycodestyle.pycqa.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

E731 false negatives

jpy-git opened this issue · comments

a = lambda x: x  # Raises E731
a = (
    lambda x: x 
) # Doesn't raise E731
a, b = lambda x: x, lambda y: y  # Doesn't raise E731
a, b = 1, lambda y: y  # Doesn't raise E731

I think the best way to resolve this is to just mark all logical_lines with an '=' that has a 'lambda' at some point after it as having a lambda assignment. This covers all cases here and I can't think of any additional unwanted cases that this would include (as long as it is not an assignment operator or function declaration annotation, but these already checked and excluded). I'll make a PR implementing this.

I think the best way to resolve this is to just mark all logical_lines with an '=' that has a 'lambda' at some point after it as having a lambda assignment.

mapped = map(lambda i: i * 2, range(10))

would also be matched by this when it shouldn't, if I understood you correctly?

Oh, you're right it is not as simple as what I had said.

mapped = map(lambda i: i * 2, range(10))

In addition to this,

f.method = lambda: 'Method'

and

f['a'] = lambda x: x ** 2

both should be allowed according to tests/E73.py
Nonetheless, I came up with a regex that should only search for actual lambda assignments (making sure to account for all of the examples above). Additionally, regex is a more elegant solution than the way it was already being implemented. Making a PR.