Skip to content

Commit 9a0bc6e

Browse files
dannyseplerasottile
authored andcommitted
E741 should work with lambdas
1 parent e119507 commit 9a0bc6e

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

pycodestyle.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,14 +1494,18 @@ def ambiguous_identifier(logical_line, tokens):
14941494
E741: I = 42
14951495
14961496
Variables can be bound in several other contexts, including class
1497-
and function definitions, 'global' and 'nonlocal' statements,
1498-
exception handlers, and 'with' and 'for' statements.
1497+
and function definitions, lambda functions, 'global' and 'nonlocal'
1498+
statements, exception handlers, and 'with' and 'for' statements.
14991499
In addition, we have a special handling for function parameters.
15001500
15011501
Okay: except AttributeError as o:
15021502
Okay: with lock as L:
15031503
Okay: foo(l=12)
1504+
Okay: foo(l=I)
15041505
Okay: for a in foo(l=12):
1506+
Okay: lambda arg: arg * l
1507+
Okay: lambda a=l[I:5]: None
1508+
Okay: lambda x=a.I: None
15051509
E741: except AttributeError as O:
15061510
E741: with lock as l:
15071511
E741: global I
@@ -1510,17 +1514,23 @@ def ambiguous_identifier(logical_line, tokens):
15101514
E741: def foo(l=12):
15111515
E741: l = foo(l=12)
15121516
E741: for l in range(10):
1517+
E741: [l for l in lines if l]
1518+
E741: lambda l: None
1519+
E741: lambda a=x[1:5], l: None
1520+
E741: lambda **l:
1521+
E741: def f(**l):
15131522
E742: class I(object):
15141523
E743: def l(x):
15151524
"""
1516-
is_func_def = False # Set to true if 'def' is found
1525+
is_func_def = False # Set to true if 'def' or 'lambda' is found
15171526
parameter_parentheses_level = 0
15181527
idents_to_avoid = ('l', 'O', 'I')
15191528
prev_type, prev_text, prev_start, prev_end, __ = tokens[0]
1520-
for token_type, text, start, end, line in tokens[1:]:
1529+
for index in range(1, len(tokens)):
1530+
token_type, text, start, end, line = tokens[index]
15211531
ident = pos = None
15221532
# find function definitions
1523-
if prev_text == 'def':
1533+
if prev_text in {'def', 'lambda'}:
15241534
is_func_def = True
15251535
# update parameter parentheses level
15261536
if parameter_parentheses_level == 0 and \
@@ -1545,11 +1555,15 @@ def ambiguous_identifier(logical_line, tokens):
15451555
if text in idents_to_avoid:
15461556
ident = text
15471557
pos = start
1548-
# function parameter definitions
1549-
if is_func_def:
1550-
if text in idents_to_avoid:
1551-
ident = text
1552-
pos = start
1558+
# function / lambda parameter definitions
1559+
if (
1560+
is_func_def and
1561+
index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and
1562+
prev_text in {'lambda', ',', '*', '**', '('} and
1563+
text in idents_to_avoid
1564+
):
1565+
ident = text
1566+
pos = start
15531567
if prev_text == 'class':
15541568
if text in idents_to_avoid:
15551569
yield start, "E742 ambiguous class definition '%s'" % text

0 commit comments

Comments
 (0)