@@ -434,24 +434,37 @@ def extraneous_whitespace(logical_line):
434434 E203: if x == 4 : print x, y; x, y = y, x
435435 """
436436
437- def is_a_slice (line , colon_position ):
438- """Check colon acts as a slice
437+ def is_a_slice (line , space_pos ):
438+ """Check if the colon after an extra space acts as a slice
439439
440440 Return True if the colon is directly contained within
441441 square brackets.
442442 """
443+ # list of found '[({' as tuples "(char, position)"
443444 parentheses_brackets_braces = list ()
444- for i in range (colon_position ):
445+ for i in range (space_pos ):
445446 c = line [i ]
446447 if c in '[({' :
447- parentheses_brackets_braces += c
448+ # add it to the stack
449+ parentheses_brackets_braces .append ((c , i ))
448450 elif c in '])}' :
449- last_opened = parentheses_brackets_braces .pop ()
451+ # unstack last item and check consistency
452+ last_opened = parentheses_brackets_braces .pop ()[0 ]
450453 expected_close = {'{' : '}' , '(' : ')' , '[' : ']' }[last_opened ]
451454 if c != expected_close : # invalid Python code
452455 return False
453- return (len (parentheses_brackets_braces ) > 0 and
454- parentheses_brackets_braces .pop () == '[' )
456+ is_within_brackets = (len (parentheses_brackets_braces ) > 0 and
457+ parentheses_brackets_braces [- 1 ][0 ] == '[' )
458+
459+ is_lambda_expr = False
460+ if is_within_brackets :
461+ # check for a lambda expression nested in brackets
462+ if space_pos > 6 :
463+ last_opened = parentheses_brackets_braces [- 1 ]
464+ between_bracket_colon = line [last_opened [1 ] + 1 : space_pos ]
465+ is_lambda_expr = 'lambda' in between_bracket_colon
466+
467+ return (is_within_brackets and not is_lambda_expr )
455468
456469 line = logical_line
457470 for match in EXTRANEOUS_WHITESPACE_REGEX .finditer (line ):
0 commit comments