Skip to content

Commit bbefb19

Browse files
Darioush JalalinasabIanLee1521
authored andcommitted
Fixing compound_statement not to be quadratic in # of :s
1 parent e73ce7d commit bbefb19

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

pep8.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -919,22 +919,21 @@ def compound_statements(logical_line):
919919
line = logical_line
920920
last_char = len(line) - 1
921921
found = line.find(':')
922+
prev_found = 0
923+
counts = dict((char, 0) for char in '{}[]()')
922924
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"
933931
break
934-
if before.startswith('def '):
932+
if line.startswith('def '):
935933
yield 0, "E704 multiple statements on one line (def)"
936934
else:
937935
yield found, "E701 multiple statements on one line (colon)"
936+
prev_found = found
938937
found = line.find(':', found + 1)
939938
found = line.find(';')
940939
while -1 < found:
@@ -1238,6 +1237,14 @@ def filename_match(filename, patterns, default=True):
12381237
return any(fnmatch(filename, pattern) for pattern in patterns)
12391238

12401239

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+
12411248
if COMMENT_WITH_NL:
12421249
def _is_eol_token(token):
12431250
return (token[0] in NEWLINE or

0 commit comments

Comments
 (0)