Skip to content

Commit b388782

Browse files
authored
Merge pull request #1123 from PyCQA/fix-E741-again
fix ambiguous identifiers in lambda bodies inside braces
2 parents 7498309 + 798d620 commit b388782

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pycodestyle.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ def ambiguous_identifier(logical_line, tokens):
15171517
E742: class I(object):
15181518
E743: def l(x):
15191519
"""
1520-
is_func_def = False # Set to true if 'def' or 'lambda' is found
1520+
func_depth = None # set to brace depth if 'def' or 'lambda' is found
15211521
seen_colon = False # set to true if we're done with function parameters
15221522
brace_depth = 0
15231523
idents_to_avoid = ('l', 'O', 'I')
@@ -1527,8 +1527,13 @@ def ambiguous_identifier(logical_line, tokens):
15271527
ident = pos = None
15281528
# find function definitions
15291529
if prev_text in {'def', 'lambda'}:
1530-
is_func_def = True
1531-
elif is_func_def and text == ':' and brace_depth == 0:
1530+
func_depth = brace_depth
1531+
seen_colon = False
1532+
elif (
1533+
func_depth is not None and
1534+
text == ':' and
1535+
brace_depth == func_depth
1536+
):
15321537
seen_colon = True
15331538
# update parameter parentheses level
15341539
if text in '([{':
@@ -1548,7 +1553,7 @@ def ambiguous_identifier(logical_line, tokens):
15481553
pos = start
15491554
# function / lambda parameter definitions
15501555
if (
1551-
is_func_def and
1556+
func_depth is not None and
15521557
not seen_colon and
15531558
index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and
15541559
prev_text in {'lambda', ',', '*', '**', '('} and

testsuite/E74.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@
22
lambda l: dict(zip(l, range(len(l))))
33
#: E741:1:7 E704:1:1
44
def f(l): print(l, l, l)
5+
#: E741:2:12
6+
x = (
7+
lambda l: dict(zip(l, range(len(l)))),
8+
)
9+
#: E741:2:12 E741:3:12
10+
x = (
11+
lambda l: dict(zip(l, range(len(l)))),
12+
lambda l: dict(zip(l, range(len(l)))),
13+
)

0 commit comments

Comments
 (0)