diff options
author | Tim Leslie <timl@breakawayconsulting.com.au> | 2012-08-20 16:02:05 +1000 |
---|---|---|
committer | Tim Leslie <timl@breakawayconsulting.com.au> | 2012-08-20 16:02:05 +1000 |
commit | a589dcea61e6fbb5b7f71501bff4701887a6ec0d (patch) | |
tree | 277b3c33142b0c6faf9513b0b4b4616242d1fa4f | |
parent | 7f2d5a3f561d48864211642097f05c6a5d2c2a55 (diff) | |
download | pep8-a589dcea61e6fbb5b7f71501bff4701887a6ec0d.tar.gz |
Add optional surrounding whitespace to high precedence mathematical operators (*, **, /, //). Addresses issue #96
-rwxr-xr-x | pep8.py | 38 | ||||
-rw-r--r-- | testsuite/E21.py | 3 | ||||
-rw-r--r-- | testsuite/E22.py | 11 |
3 files changed, 33 insertions, 19 deletions
@@ -126,12 +126,13 @@ REPORT_FORMAT = { SINGLETONS = frozenset(['False', 'None', 'True']) KEYWORDS = frozenset(keyword.kwlist + ['print']) - SINGLETONS -BINARY_OPERATORS = frozenset([ +WS_BINARY_OPERATORS = frozenset([ '**=', '*=', '+=', '-=', '!=', '<>', '%=', '^=', '&=', '|=', '==', '/=', '//=', '<=', '>=', '<<=', '>>=', - '%', '^', '&', '|', '=', '/', '//', '<', '>', '<<']) + '%', '^', '&', '|', '=', '<', '>', '<<']) +WS_OPTIONAL_OPERATORS = frozenset([ + '**', '*', '/', '//']) UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-']) -OPERATORS = BINARY_OPERATORS | UNARY_OPERATORS WHITESPACE = frozenset(' \t') SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT]) @@ -671,11 +672,13 @@ def missing_whitespace_around_operator(logical_line, tokens): Okay: alpha[:-i] Okay: if not -5 < x < +5:\n pass Okay: lambda *args, **kw: (args, kw) + Okay: z = 2**30 + Okay: z = 2 ** 30 + Okay: x = x*2 - 1 + Okay: hypot2 = x*x + y*y E225: i=i+1 E225: submitted +=1 - E225: x = x*2 - 1 - E225: hypot2 = x*x + y*y E225: c = (a+b) * (a-b) E225: c = alpha -4 E225: z = x **y @@ -694,32 +697,49 @@ def missing_whitespace_around_operator(logical_line, tokens): parens -= 1 if need_space: if start != prev_end: + # Found a needed space need_space = False elif text == '>' and prev_text in ('<', '-'): # Tolerate the "<>" operator, even if running Python 3 # Deal with Python 3's annotated return value "->" pass else: + # A needed trailing space was not found yield prev_end, "E225 missing whitespace around operator" need_space = False elif token_type == tokenize.OP and prev_end is not None: if text == '=' and parens: # Allow keyword args or defaults: foo(bar=None). pass - elif text in BINARY_OPERATORS: + elif text in WS_BINARY_OPERATORS: need_space = True elif text in UNARY_OPERATORS: + # Check if the operator is being used in as a binary operator # Allow unary operators: -123, -x, +1. # Allow argument unpacking: foo(*args, **kwargs). + binary_usage = False if prev_type == tokenize.OP: if prev_text in '}])': - need_space = True + binary_usage = True elif prev_type == tokenize.NAME: if prev_text not in KEYWORDS: - need_space = True + binary_usage = True elif prev_type not in SKIP_TOKENS: - need_space = True + binary_usage = True + + if binary_usage: + #print "BINARY", prev_text, text + if text in WS_OPTIONAL_OPERATORS: + # Surrounding space is optional + # Ensure trailing space matches opening space + #print "OPTIONAL", text + need_space = start != prev_end + #print "Need space?", need_space + else: + need_space = True + if need_space and start == prev_end: + # A needed opening space was not found yield prev_end, "E225 missing whitespace around operator" need_space = False prev_type = token_type diff --git a/testsuite/E21.py b/testsuite/E21.py index 2280caf..e830895 100644 --- a/testsuite/E21.py +++ b/testsuite/E21.py @@ -4,9 +4,6 @@ spam (1) dict ['key'] = list [index] #: E211 dict['key'] ['subkey'] = list[index] -#: E225 -def squares(n): - return (i**2 for i in range(n)) #: Okay spam(1) dict['key'] = list[index] diff --git a/testsuite/E22.py b/testsuite/E22.py index a6916ff..0583e68 100644 --- a/testsuite/E22.py +++ b/testsuite/E22.py @@ -51,14 +51,8 @@ i=i+1 #: E225 submitted +=1 #: E225 -x = x*2 - 1 -#: E225 -hypot2 = x*x + y*y -#: E225 c = (a+b) * (a-b) #: E225 -c = (a + b)*(a - b) -#: E225 c = (a +b)*(a - b) #: E225 c =-1 @@ -69,15 +63,18 @@ z = (x + 1) **y #: E225 norman = True+False #: E225 -_1MB = 2 ** 20 _1kB = _1MB >>10 #: #: Okay i = i + 1 submitted += 1 x = x * 2 - 1 +x = x*2 - 1 hypot2 = x * x + y * y +hypot2 = x*x + y*y c = (a + b) * (a - b) +c = (a + b)*(a - b) +_1MB = 2 ** 20 foo(bar, key='word', *args, **kwargs) baz(**kwargs) negative = -1 |