Skip to content

Commit c5308a7

Browse files
authored
Merge pull request #1122 from PyCQA/stop-E741-after-parameters
fix reporting of ambiguous identifier after parameter list
2 parents 6cac99d + 56dac13 commit c5308a7

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

pycodestyle.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,8 @@ def ambiguous_identifier(logical_line, tokens):
15221522
E743: def l(x):
15231523
"""
15241524
is_func_def = False # Set to true if 'def' or 'lambda' is found
1525-
parameter_parentheses_level = 0
1525+
seen_colon = False # set to true if we're done with function parameters
1526+
brace_depth = 0
15261527
idents_to_avoid = ('l', 'O', 'I')
15271528
prev_type, prev_text, prev_start, prev_end, __ = tokens[0]
15281529
for index in range(1, len(tokens)):
@@ -1531,20 +1532,15 @@ def ambiguous_identifier(logical_line, tokens):
15311532
# find function definitions
15321533
if prev_text in {'def', 'lambda'}:
15331534
is_func_def = True
1535+
elif is_func_def and text == ':' and brace_depth == 0:
1536+
seen_colon = True
15341537
# update parameter parentheses level
1535-
if parameter_parentheses_level == 0 and \
1536-
prev_type == tokenize.NAME and \
1537-
token_type == tokenize.OP and text == '(':
1538-
parameter_parentheses_level = 1
1539-
elif parameter_parentheses_level > 0 and \
1540-
token_type == tokenize.OP:
1541-
if text == '(':
1542-
parameter_parentheses_level += 1
1543-
elif text == ')':
1544-
parameter_parentheses_level -= 1
1538+
if text in '([{':
1539+
brace_depth += 1
1540+
elif text in ')]}':
1541+
brace_depth -= 1
15451542
# identifiers on the lhs of an assignment operator
1546-
if token_type == tokenize.OP and text in {'=', ':='} and \
1547-
parameter_parentheses_level == 0:
1543+
if text == ':=' or (text == '=' and brace_depth == 0):
15481544
if prev_text in idents_to_avoid:
15491545
ident = prev_text
15501546
pos = prev_start
@@ -1557,6 +1553,7 @@ def ambiguous_identifier(logical_line, tokens):
15571553
# function / lambda parameter definitions
15581554
if (
15591555
is_func_def and
1556+
not seen_colon and
15601557
index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and
15611558
prev_text in {'lambda', ',', '*', '**', '('} and
15621559
text in idents_to_avoid
@@ -1571,7 +1568,6 @@ def ambiguous_identifier(logical_line, tokens):
15711568
yield start, "E743 ambiguous function definition '%s'" % text
15721569
if ident:
15731570
yield pos, "E741 ambiguous variable name '%s'" % ident
1574-
prev_type = token_type
15751571
prev_text = text
15761572
prev_start = start
15771573

testsuite/E74.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#: E741:1:8
2+
lambda l: dict(zip(l, range(len(l))))
3+
#: E741:1:7 E704:1:1
4+
def f(l): print(l, l, l)

testsuite/python38.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ def f3(
5656
#: E741
5757
while l := 1:
5858
pass
59+
#: E741
60+
if (l := 1):
61+
pass

0 commit comments

Comments
 (0)