@@ -919,22 +919,21 @@ def compound_statements(logical_line):
919
919
line = logical_line
920
920
last_char = len (line ) - 1
921
921
found = line .find (':' )
922
+ prev_found = 0
923
+ counts = dict ((char , 0 ) for char in '{}[]()' )
922
924
while - 1 < found < last_char :
923
- before = line [:found ]
924
- if ((before .count ('{' ) <= before .count ('}' ) and # {'a': 1} (dict)
925
- before .count ('[' ) <= before .count (']' ) and # [1:2] (slice)
926
- before .count ('(' ) <= before .count (')' ))): # (annotation)
927
- lambda_kw = LAMBDA_REGEX .search (before )
928
- if lambda_kw :
929
- before = line [:lambda_kw .start ()].rstrip ()
930
- if before [- 1 :] == '=' and isidentifier (before [:- 1 ].strip ()):
931
- yield 0 , ("E731 do not assign a lambda expression, use a "
932
- "def" )
925
+ update_counts (line [prev_found :found ], counts )
926
+ if ((counts ['{' ] <= counts ['}' ] and # {'a': 1} (dict)
927
+ counts ['[' ] <= counts [']' ] and # [1:2] (slice)
928
+ counts ['(' ] <= counts [')' ])): # (annotation)
929
+ if LAMBDA_REGEX .search (line , 0 , found ):
930
+ yield 0 , "E731 do not assign a lambda expression, use a def"
933
931
break
934
- if before .startswith ('def ' ):
932
+ if line .startswith ('def ' ):
935
933
yield 0 , "E704 multiple statements on one line (def)"
936
934
else :
937
935
yield found , "E701 multiple statements on one line (colon)"
936
+ prev_found = found
938
937
found = line .find (':' , found + 1 )
939
938
found = line .find (';' )
940
939
while - 1 < found :
@@ -1238,6 +1237,14 @@ def filename_match(filename, patterns, default=True):
1238
1237
return any (fnmatch (filename , pattern ) for pattern in patterns )
1239
1238
1240
1239
1240
+ def update_counts (s , counts ):
1241
+ r"""Adds one to the counts of each appearence of characters in s,
1242
+ for characters in counts"""
1243
+ for char in s :
1244
+ if char in counts :
1245
+ counts [char ] += 1
1246
+
1247
+
1241
1248
if COMMENT_WITH_NL :
1242
1249
def _is_eol_token (token ):
1243
1250
return (token [0 ] in NEWLINE or
0 commit comments