diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2012-12-20 17:38:42 +0100 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2012-12-20 17:38:42 +0100 |
commit | dea67b73c13878e4a993f3d09c9019f67e5d4330 (patch) | |
tree | bcc14379331fdab358136eb83ed5730a0039ad2a | |
parent | 7784a77ffb9e95aeb63d465b224e0189a788bb14 (diff) | |
download | pep8-dea67b73c13878e4a993f3d09c9019f67e5d4330.tar.gz |
Regroup optional E225 checks with new error code E226 which is ignored in the default configuration. issue #96
-rw-r--r-- | CHANGES.txt | 7 | ||||
-rwxr-xr-x | pep8.py | 42 | ||||
-rw-r--r-- | testsuite/E22.py | 45 |
3 files changed, 65 insertions, 29 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 17a62ae..50d0c89 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,10 +5,13 @@ Changelog 1.x (UNRELEASED) ---------------- -* Relax E225 for high priority operators (``*``, ``**``, ``/``). (Issue #96) +* Report E226 instead of E225 for optional white space around common + operators (``*``, ``**``, ``/``, ``+`` and ``-``). This new error code + is ignored in the default configuration because PEP 8 recommends to + "use your own judgement". (Issue #96) * Lines with a ``# nopep8`` at the end will not issue errors on line length - ``E501`` or continuation line indentation ``E12*``. (Issue #27) + E501 or continuation line indentation E12*. (Issue #27) 1.3.4 (2012-12-18) @@ -112,7 +112,7 @@ except ImportError: from ConfigParser import RawConfigParser DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git' -DEFAULT_IGNORE = 'E24' +DEFAULT_IGNORE = 'E226,E24' if sys.platform == 'win32': DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8') else: @@ -132,7 +132,7 @@ WS_NEEDED_OPERATORS = frozenset([ '%=', '^=', '&=', '|=', '==', '/=', '//=', '<=', '>=', '<<=', '>>=', '%', '^', '&', '|', '=', '<', '>', '<<']) WS_OPTIONAL_OPERATORS = frozenset([ - '**', '*', '/', '//']) + '**', '*', '/', '//', '+', '-']) UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-']) WHITESPACE = frozenset(' \t') SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE, @@ -681,19 +681,19 @@ 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: c = (a+b) * (a-b) E225: c = alpha -4 E225: x = x /2 - 1 E225: z = x **y + E226: c = (a+b) * (a-b) + E226: z = 2**30 + E226: x = x*2 - 1 + E226: x = x/2 - 1 + E226: hypot2 = x*x + y*y """ parens = 0 need_space = False @@ -709,15 +709,22 @@ def missing_whitespace_around_operator(logical_line, tokens): parens -= 1 if need_space: if start != prev_end: - # Found a needed space + # Found a (probably) needed space + if need_space is not True and not need_space[1]: + yield (need_space[0], + "E225 missing whitespace around operator") 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" + if need_space is True or need_space[1]: + # A needed trailing space was not found + yield prev_end, "E225 missing whitespace around operator" + else: + yield (need_space[0], + "E226 missing optional whitespace around operator") need_space = False elif token_type == tokenize.OP and prev_end is not None: if text == '=' and parens: @@ -741,20 +748,21 @@ def missing_whitespace_around_operator(logical_line, tokens): if binary_usage: if text in WS_OPTIONAL_OPERATORS: - # Surrounding space is optional - # Ensure trailing space matches opening space - need_space = start != prev_end + need_space = 'option' 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 + need_space = 'option' - if need_space and start == prev_end: + if need_space == 'option': + # Surrounding space is optional, but ensure that + # trailing space matches opening space + need_space = (prev_end, start != prev_end) + elif 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 prev_text = text prev_end = end diff --git a/testsuite/E22.py b/testsuite/E22.py index 96393d0..36e028e 100644 --- a/testsuite/E22.py +++ b/testsuite/E22.py @@ -47,33 +47,58 @@ b += 1000 #: E225 -i=i+1 -#: E225 submitted +=1 #: E225 -c = (a+b) * (a-b) -#: E225 -c = (a +b)*(a - b) +submitted+= 1 #: E225 c =-1 #: E225 c = alpha -4 #: E225 +c = alpha- 4 +#: E225 z = (x + 1) **y #: E225 -norman = True+False +z = (x + 1)** y #: E225 _1kB = _1MB >>10 +#: E225 +_1kB = _1MB>> 10 +#: E225 +i=i+ 1 +#: E225 +i=i +1 +#: E225 E226 +i=i+1 +#: E225 E226 +i =i+1 +#: E225 E226 +i= i+1 +#: E225 E226 +c = (a +b)*(a - b) +#: E225 E226 +c = (a+ b)*(a - b) +#: +#: E226 +c = (a+b) * (a-b) +#: E226 +norman = True+False +#: E226 +x = x*2 - 1 +#: E226 +hypot2 = x*x + y*y +#: E226 +c = (a + b)*(a - b) +#: E226 +def squares(n): + return (i**2 for i in range(n)) #: #: 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) @@ -91,5 +116,5 @@ if True: def squares(n): - return (i**2 for i in range(n)) + return (i ** 2 for i in range(n)) #: |