Skip to content

Commit 798d620

Browse files
committed
fix ambiguous identifiers in lambda bodies inside braces
1 parent c5308a7 commit 798d620

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
@@ -1521,7 +1521,7 @@ def ambiguous_identifier(logical_line, tokens):
15211521
E742: class I(object):
15221522
E743: def l(x):
15231523
"""
1524-
is_func_def = False # Set to true if 'def' or 'lambda' is found
1524+
func_depth = None # set to brace depth if 'def' or 'lambda' is found
15251525
seen_colon = False # set to true if we're done with function parameters
15261526
brace_depth = 0
15271527
idents_to_avoid = ('l', 'O', 'I')
@@ -1531,8 +1531,13 @@ def ambiguous_identifier(logical_line, tokens):
15311531
ident = pos = None
15321532
# find function definitions
15331533
if prev_text in {'def', 'lambda'}:
1534-
is_func_def = True
1535-
elif is_func_def and text == ':' and brace_depth == 0:
1534+
func_depth = brace_depth
1535+
seen_colon = False
1536+
elif (
1537+
func_depth is not None and
1538+
text == ':' and
1539+
brace_depth == func_depth
1540+
):
15361541
seen_colon = True
15371542
# update parameter parentheses level
15381543
if text in '([{':
@@ -1552,7 +1557,7 @@ def ambiguous_identifier(logical_line, tokens):
15521557
pos = start
15531558
# function / lambda parameter definitions
15541559
if (
1555-
is_func_def and
1560+
func_depth is not None and
15561561
not seen_colon and
15571562
index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and
15581563
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)