PyCQA / pycodestyle

Simple Python style checker in one Python file
https://pycodestyle.pycqa.org
Other
5.03k stars 753 forks source link

E731 false negatives #1061

Open jpy-git opened 2 years ago

jpy-git commented 2 years ago
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
Sam-Cleveland commented 2 years ago

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.

FichteFoll commented 2 years ago

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?

Sam-Cleveland commented 2 years ago

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.