Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing false negatives with E731 lambda assignments #1078

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@
)
KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+|:=)(\s*)')
LAMBDA_REGEX = re.compile(r'\blambda\b')
LAMBDA_ASSIGNMENT_REGEX = re.compile(
r'(?<!\.)\w+\s*(\,\s*\w+\s*)*\='
r'(.*\,)*[\s|\(]*lambda')
HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)\b')
STARTSWITH_TOP_LEVEL_REGEX = re.compile(r'^(async\s+def\s+|def\s+|class\s+|@)')
Expand Down Expand Up @@ -1203,20 +1205,16 @@ def compound_statements(logical_line):
found = line.find(':')
prev_found = 0
counts = {char: 0 for char in '{}[]()'}
if LAMBDA_ASSIGNMENT_REGEX.match(line):
yield 0, ("E731 do not assign a lambda expression, use a "
"def")
while -1 < found < last_char:
update_counts(line[prev_found:found], counts)
if ((counts['{'] <= counts['}'] and # {'a': 1} (dict)
counts['['] <= counts[']'] and # [1:2] (slice)
counts['('] <= counts[')']) and # (annotation)
not (sys.version_info >= (3, 8) and
line[found + 1] == '=')): # assignment expression
lambda_kw = LAMBDA_REGEX.search(line, 0, found)
if lambda_kw:
before = line[:lambda_kw.start()].rstrip()
if before[-1:] == '=' and before[:-1].strip().isidentifier():
yield 0, ("E731 do not assign a lambda expression, use a "
"def")
break
if STARTSWITH_DEF_REGEX.match(line):
yield 0, "E704 multiple statements on one line (def)"
elif STARTSWITH_INDENT_STATEMENT_REGEX.match(line):
Expand Down