@@ -434,24 +434,37 @@ def extraneous_whitespace(logical_line):
434
434
E203: if x == 4 : print x, y; x, y = y, x
435
435
"""
436
436
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
439
439
440
440
Return True if the colon is directly contained within
441
441
square brackets.
442
442
"""
443
+ # list of found '[({' as tuples "(char, position)"
443
444
parentheses_brackets_braces = list ()
444
- for i in range (colon_position ):
445
+ for i in range (space_pos ):
445
446
c = line [i ]
446
447
if c in '[({' :
447
- parentheses_brackets_braces += c
448
+ # add it to the stack
449
+ parentheses_brackets_braces .append ((c , i ))
448
450
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 ]
450
453
expected_close = {'{' : '}' , '(' : ')' , '[' : ']' }[last_opened ]
451
454
if c != expected_close : # invalid Python code
452
455
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 )
455
468
456
469
line = logical_line
457
470
for match in EXTRANEOUS_WHITESPACE_REGEX .finditer (line ):
0 commit comments