Skip to content

Commit

Permalink
fix ambiguous identifiers in lambda bodies inside braces
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Nov 21, 2022
1 parent c5308a7 commit 0af9b37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pycodestyle.py
Expand Up @@ -1521,7 +1521,7 @@ def ambiguous_identifier(logical_line, tokens):
E742: class I(object):
E743: def l(x):
"""
is_func_def = False # Set to true if 'def' or 'lambda' is found
func_depth = None # set to brace depth if 'def' or 'lambda' is found
seen_colon = False # set to true if we're done with function parameters
brace_depth = 0
idents_to_avoid = ('l', 'O', 'I')
Expand All @@ -1531,8 +1531,12 @@ def ambiguous_identifier(logical_line, tokens):
ident = pos = None
# find function definitions
if prev_text in {'def', 'lambda'}:
is_func_def = True
elif is_func_def and text == ':' and brace_depth == 0:
func_depth = brace_depth
elif (
func_depth is not None and
text == ':' and
brace_depth == func_depth
):
seen_colon = True
# update parameter parentheses level
if text in '([{':
Expand All @@ -1552,7 +1556,7 @@ def ambiguous_identifier(logical_line, tokens):
pos = start
# function / lambda parameter definitions
if (
is_func_def and
func_depth is not None and
not seen_colon and
index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and
prev_text in {'lambda', ',', '*', '**', '('} and
Expand Down
4 changes: 4 additions & 0 deletions testsuite/E74.py
Expand Up @@ -2,3 +2,7 @@
lambda l: dict(zip(l, range(len(l))))
#: E741:1:7 E704:1:1
def f(l): print(l, l, l)
#: E741:2:12
x = (
lambda l: dict(zip(l, range(len(l)))),
)

0 comments on commit 0af9b37

Please sign in to comment.