diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2012-12-19 01:16:30 +0100 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2012-12-19 01:16:30 +0100 |
commit | 7d3efa228bed40b3b7c1881bd42546e2f9ba6636 (patch) | |
tree | d292c77b63114c75d412c831cc3e4dea94c3b2c5 | |
parent | 1a5dfd0b3df6fe36d10d8199f62c7e03abc52a8a (diff) | |
parent | a6085dfb4c81cb89e40b07e61e7ff143111775dd (diff) | |
download | pep8-7d3efa228bed40b3b7c1881bd42546e2f9ba6636.tar.gz |
Merge pull request #123 to adapt E225 to the latest recommendations.
-rwxr-xr-x | pep8.py | 42 | ||||
-rw-r--r-- | testsuite/E21.py | 3 | ||||
-rw-r--r-- | testsuite/E22.py | 11 |
3 files changed, 37 insertions, 19 deletions
@@ -127,12 +127,13 @@ REPORT_FORMAT = { SINGLETONS = frozenset(['False', 'None', 'True']) KEYWORDS = frozenset(keyword.kwlist + ['print']) - SINGLETONS -BINARY_OPERATORS = frozenset([ +WS_NEEDED_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]) @@ -675,13 +676,18 @@ 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: x = x / 2 - 1 + 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: x = x /2 - 1 E225: z = x **y """ parens = 0 @@ -698,32 +704,50 @@ 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_NEEDED_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: + if text in WS_OPTIONAL_OPERATORS: + # Surrounding space is optional + # Ensure trailing space matches opening space + need_space = start != prev_end + else: + need_space = True + elif text in WS_OPTIONAL_OPERATORS: + # Surrounding space is optional + # Ensure trailing space matches opening space + need_space = start != prev_end + 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 |