diff --git a/pycodestyle.py b/pycodestyle.py index a105a0f3..7ee2375a 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1522,7 +1522,8 @@ def ambiguous_identifier(logical_line, tokens): E743: def l(x): """ is_func_def = False # Set to true if 'def' or 'lambda' is found - parameter_parentheses_level = 0 + seen_colon = False # set to true if we're done with function parameters + brace_depth = 0 idents_to_avoid = ('l', 'O', 'I') prev_type, prev_text, prev_start, prev_end, __ = tokens[0] for index in range(1, len(tokens)): @@ -1531,20 +1532,15 @@ def ambiguous_identifier(logical_line, tokens): # find function definitions if prev_text in {'def', 'lambda'}: is_func_def = True + elif is_func_def and text == ':' and brace_depth == 0: + seen_colon = True # update parameter parentheses level - if parameter_parentheses_level == 0 and \ - prev_type == tokenize.NAME and \ - token_type == tokenize.OP and text == '(': - parameter_parentheses_level = 1 - elif parameter_parentheses_level > 0 and \ - token_type == tokenize.OP: - if text == '(': - parameter_parentheses_level += 1 - elif text == ')': - parameter_parentheses_level -= 1 + if text in '([{': + brace_depth += 1 + elif text in ')]}': + brace_depth -= 1 # identifiers on the lhs of an assignment operator - if token_type == tokenize.OP and text in {'=', ':='} and \ - parameter_parentheses_level == 0: + if text == ':=' or (text == '=' and brace_depth == 0): if prev_text in idents_to_avoid: ident = prev_text pos = prev_start @@ -1557,6 +1553,7 @@ def ambiguous_identifier(logical_line, tokens): # function / lambda parameter definitions if ( is_func_def and + not seen_colon and index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and prev_text in {'lambda', ',', '*', '**', '('} and text in idents_to_avoid @@ -1571,7 +1568,6 @@ def ambiguous_identifier(logical_line, tokens): yield start, "E743 ambiguous function definition '%s'" % text if ident: yield pos, "E741 ambiguous variable name '%s'" % ident - prev_type = token_type prev_text = text prev_start = start diff --git a/testsuite/E74.py b/testsuite/E74.py new file mode 100644 index 00000000..93d6c131 --- /dev/null +++ b/testsuite/E74.py @@ -0,0 +1,4 @@ +#: E741:1:8 +lambda l: dict(zip(l, range(len(l)))) +#: E741:1:7 E704:1:1 +def f(l): print(l, l, l) diff --git a/testsuite/python38.py b/testsuite/python38.py index faf9aa7a..536448d8 100644 --- a/testsuite/python38.py +++ b/testsuite/python38.py @@ -56,3 +56,6 @@ def f3( #: E741 while l := 1: pass +#: E741 +if (l := 1): + pass