@@ -958,22 +958,25 @@ def compound_statements(logical_line):
958
958
line = logical_line
959
959
last_char = len (line ) - 1
960
960
found = line .find (':' )
961
+ prev_found = 0
962
+ counts = dict ((char , 0 ) for char in '{}[]()' )
961
963
while - 1 < found < last_char :
962
- before = line [:found ]
963
- if ((before . count ( '{' ) <= before . count ( '}' ) and # {'a': 1} (dict)
964
- before . count ( '[' ) <= before . count ( ']' ) and # [1:2] (slice)
965
- before . count ( '(' ) <= before . count ( ')' ) )): # (annotation)
966
- lambda_kw = LAMBDA_REGEX .search (before )
964
+ update_counts ( line [prev_found :found ], counts )
965
+ if ((counts [ '{' ] <= counts [ '}' ] and # {'a': 1} (dict)
966
+ counts [ '[' ] <= counts [ ']' ] and # [1:2] (slice)
967
+ counts [ '(' ] <= counts [ ')' ] )): # (annotation)
968
+ lambda_kw = LAMBDA_REGEX .search (line , 0 , found )
967
969
if lambda_kw :
968
970
before = line [:lambda_kw .start ()].rstrip ()
969
971
if before [- 1 :] == '=' and isidentifier (before [:- 1 ].strip ()):
970
972
yield 0 , ("E731 do not assign a lambda expression, use a "
971
973
"def" )
972
974
break
973
- if before .startswith ('def ' ):
975
+ if line .startswith ('def ' ):
974
976
yield 0 , "E704 multiple statements on one line (def)"
975
977
else :
976
978
yield found , "E701 multiple statements on one line (colon)"
979
+ prev_found = found
977
980
found = line .find (':' , found + 1 )
978
981
found = line .find (';' )
979
982
while - 1 < found :
@@ -1333,8 +1336,18 @@ def filename_match(filename, patterns, default=True):
1333
1336
return any (fnmatch (filename , pattern ) for pattern in patterns )
1334
1337
1335
1338
1339
+ def update_counts (s , counts ):
1340
+ r"""Adds one to the counts of each appearence of characters in s,
1341
+ for characters in counts"""
1342
+ for char in s :
1343
+ if char in counts :
1344
+ counts [char ] += 1
1345
+
1346
+
1336
1347
def _is_eol_token (token ):
1337
1348
return token [0 ] in NEWLINE or token [4 ][token [3 ][1 ]:].lstrip () == '\\ \n '
1349
+
1350
+
1338
1351
if COMMENT_WITH_NL :
1339
1352
def _is_eol_token (token , _eol_token = _is_eol_token ):
1340
1353
return _eol_token (token ) or (token [0 ] == tokenize .COMMENT and
0 commit comments