Skip to content

Commit b8db33d

Browse files
committed
Fixes some issues with E741 detection
1 parent 1a3f827 commit b8db33d

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

pycodestyle.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,28 +1423,55 @@ def ambiguous_identifier(logical_line, tokens):
14231423
14241424
Variables can be bound in several other contexts, including class
14251425
and function definitions, 'global' and 'nonlocal' statements,
1426-
exception handlers, and 'with' statements.
1426+
exception handlers, and 'with' and 'for' statements.
1427+
In addition, we have a special handling for function parameters.
14271428
14281429
Okay: except AttributeError as o:
14291430
Okay: with lock as L:
1431+
Okay: foo(l=12)
1432+
Okay: for a in foo(l=12):
14301433
E741: except AttributeError as O:
14311434
E741: with lock as l:
14321435
E741: global I
14331436
E741: nonlocal l
1437+
E741: def foo(l):
1438+
E741: def foo(l=12):
1439+
E741: l = foo(l=12)
1440+
E741: for l in range(10):
14341441
E742: class I(object):
14351442
E743: def l(x):
14361443
"""
1444+
is_func_def = False # Set to true if 'def' is found
1445+
parameter_parentheses_level = 0
14371446
idents_to_avoid = ('l', 'O', 'I')
14381447
prev_type, prev_text, prev_start, prev_end, __ = tokens[0]
14391448
for token_type, text, start, end, line in tokens[1:]:
14401449
ident = pos = None
1450+
# find function definitions
1451+
if prev_text == 'def':
1452+
is_func_def = True
1453+
# update parameter parentheses level
1454+
if parameter_parentheses_level == 0 and prev_type == tokenize.NAME and \
1455+
token_type == tokenize.OP and text == '(':
1456+
parameter_parentheses_level = 1
1457+
elif parameter_parentheses_level > 0 and token_type == tokenize.OP:
1458+
if text == '(':
1459+
parameter_parentheses_level += 1
1460+
elif text == ')':
1461+
parameter_parentheses_level -= 1
14411462
# identifiers on the lhs of an assignment operator
1442-
if token_type == tokenize.OP and '=' in text:
1463+
if token_type == tokenize.OP and '=' in text and \
1464+
parameter_parentheses_level == 0:
14431465
if prev_text in idents_to_avoid:
14441466
ident = prev_text
14451467
pos = prev_start
1446-
# identifiers bound to values with 'as', 'global', or 'nonlocal'
1447-
if prev_text in ('as', 'global', 'nonlocal'):
1468+
# identifiers bound to values with 'as', 'for', 'global', or 'nonlocal'
1469+
if prev_text in ('as', 'for', 'global', 'nonlocal'):
1470+
if text in idents_to_avoid:
1471+
ident = text
1472+
pos = start
1473+
# function parameter definitions
1474+
if is_func_def:
14481475
if text in idents_to_avoid:
14491476
ident = text
14501477
pos = start
@@ -1456,6 +1483,7 @@ def ambiguous_identifier(logical_line, tokens):
14561483
yield start, "E743 ambiguous function definition '%s'" % text
14571484
if ident:
14581485
yield pos, "E741 ambiguous variable name '%s'" % ident
1486+
prev_type = token_type
14591487
prev_text = text
14601488
prev_start = start
14611489

0 commit comments

Comments
 (0)