summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarioush Jalalinasab <darioush@yelp.com>2014-08-08 14:00:00 -0700
committerIan Lee <IanLee1521@gmail.com>2014-12-18 00:50:32 -0800
commitbbefb19a036e1048dbdd460432dc1f973be63504 (patch)
tree449f032db5d43e97472196d122cda01104457c0d
parente73ce7d2001f9cd3d1d6acdd1040b7e6abca77fe (diff)
downloadpep8-issue-314.tar.gz
Fixing compound_statement not to be quadratic in # of :sissue-314
-rwxr-xr-xpep8.py29
1 files changed, 18 insertions, 11 deletions
diff --git a/pep8.py b/pep8.py
index bb46c22..79a81ed 100755
--- a/pep8.py
+++ b/pep8.py
@@ -919,22 +919,21 @@ def compound_statements(logical_line):
line = logical_line
last_char = len(line) - 1
found = line.find(':')
+ prev_found = 0
+ counts = dict((char, 0) for char in '{}[]()')
while -1 < found < last_char:
- before = line[:found]
- if ((before.count('{') <= before.count('}') and # {'a': 1} (dict)
- before.count('[') <= before.count(']') and # [1:2] (slice)
- before.count('(') <= before.count(')'))): # (annotation)
- lambda_kw = LAMBDA_REGEX.search(before)
- if lambda_kw:
- before = line[:lambda_kw.start()].rstrip()
- if before[-1:] == '=' and isidentifier(before[:-1].strip()):
- yield 0, ("E731 do not assign a lambda expression, use a "
- "def")
+ update_counts(line[prev_found:found], counts)
+ if ((counts['{'] <= counts['}'] and # {'a': 1} (dict)
+ counts['['] <= counts[']'] and # [1:2] (slice)
+ counts['('] <= counts[')'])): # (annotation)
+ if LAMBDA_REGEX.search(line, 0, found):
+ yield 0, "E731 do not assign a lambda expression, use a def"
break
- if before.startswith('def '):
+ if line.startswith('def '):
yield 0, "E704 multiple statements on one line (def)"
else:
yield found, "E701 multiple statements on one line (colon)"
+ prev_found = found
found = line.find(':', found + 1)
found = line.find(';')
while -1 < found:
@@ -1238,6 +1237,14 @@ def filename_match(filename, patterns, default=True):
return any(fnmatch(filename, pattern) for pattern in patterns)
+def update_counts(s, counts):
+ r"""Adds one to the counts of each appearence of characters in s,
+ for characters in counts"""
+ for char in s:
+ if char in counts:
+ counts[char] += 1
+
+
if COMMENT_WITH_NL:
def _is_eol_token(token):
return (token[0] in NEWLINE or