diff --git a/pycodestyle.py b/pycodestyle.py index b75eed00..f06f9231 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1517,7 +1517,7 @@ def ambiguous_identifier(logical_line, tokens): E742: class I(object): E743: def l(x): """ - is_func_def = False # Set to true if 'def' or 'lambda' is found + func_depth = None # set to brace depth if 'def' or 'lambda' is found seen_colon = False # set to true if we're done with function parameters brace_depth = 0 idents_to_avoid = ('l', 'O', 'I') @@ -1527,8 +1527,13 @@ def ambiguous_identifier(logical_line, tokens): ident = pos = None # find function definitions if prev_text in {'def', 'lambda'}: - is_func_def = True - elif is_func_def and text == ':' and brace_depth == 0: + func_depth = brace_depth + seen_colon = False + elif ( + func_depth is not None and + text == ':' and + brace_depth == func_depth + ): seen_colon = True # update parameter parentheses level if text in '([{': @@ -1548,7 +1553,7 @@ def ambiguous_identifier(logical_line, tokens): pos = start # function / lambda parameter definitions if ( - is_func_def and + func_depth is not None and not seen_colon and index < len(tokens) - 1 and tokens[index + 1][1] in ':,=)' and prev_text in {'lambda', ',', '*', '**', '('} and diff --git a/testsuite/E74.py b/testsuite/E74.py index 93d6c131..9bb4c581 100644 --- a/testsuite/E74.py +++ b/testsuite/E74.py @@ -2,3 +2,12 @@ lambda l: dict(zip(l, range(len(l)))) #: E741:1:7 E704:1:1 def f(l): print(l, l, l) +#: E741:2:12 +x = ( + lambda l: dict(zip(l, range(len(l)))), +) +#: E741:2:12 E741:3:12 +x = ( + lambda l: dict(zip(l, range(len(l)))), + lambda l: dict(zip(l, range(len(l)))), +)