diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2013-02-24 13:07:53 +0100 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2013-02-24 13:07:53 +0100 |
commit | 72269ddfebfbe7075079b224df30c7c989aa7281 (patch) | |
tree | 55f0482f7e891f9420d3c34a65d5a31365a7de06 | |
parent | 2a82c7eb2afac5ef340a509b6b72acea13c36fa6 (diff) | |
download | pep8-72269ddfebfbe7075079b224df30c7c989aa7281.tar.gz |
Report E227 or E228 instead of E225 for whitespace around bitwise, shift or modulo operators; issue #166
-rw-r--r-- | CHANGES.txt | 8 | ||||
-rw-r--r-- | docs/intro.rst | 6 | ||||
-rwxr-xr-x | pep8.py | 30 | ||||
-rw-r--r-- | testsuite/E22.py | 30 |
4 files changed, 54 insertions, 20 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 9d32e7a..e87e14e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,12 @@ Changelog 1.4.x (unreleased) ------------------ +* Report E227 or E228 instead of E225 for whitespace around bitwise, shift + or modulo operators. (Issue #166) + +* Change the message for E226 to make clear that it is about arithmetic + operators. + * Fix regression with the ``--diff`` option. (Issue #169) * Fix the ``TestReport`` class to print the unexpected warnings and @@ -63,7 +69,7 @@ Changelog 1.4 (2012-12-22) ---------------- -* Report E226 instead of E225 for optional white space around common +* Report E226 instead of E225 for optional whitespace 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) diff --git a/docs/intro.rst b/docs/intro.rst index 415212d..a253901 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -230,7 +230,11 @@ This is the current list of error and warning codes: +----------+----------------------------------------------------------------------+ | E225 | missing whitespace around operator | +----------+----------------------------------------------------------------------+ -| E226 (*) | missing optional whitespace around operator | +| E226 (*) | missing whitespace around arithmetic operator | ++----------+----------------------------------------------------------------------+ +| E227 | missing whitespace around bitwise or shift operator | ++----------+----------------------------------------------------------------------+ +| E228 | missing whitespace around modulo operator | +----------+----------------------------------------------------------------------+ +----------+----------------------------------------------------------------------+ | E231 | missing whitespace after ',' | @@ -81,11 +81,11 @@ PyCF_ONLY_AST = 1024 SINGLETONS = frozenset(['False', 'None', 'True']) KEYWORDS = frozenset(keyword.kwlist + ['print']) - SINGLETONS UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-']) -WS_OPTIONAL_OPERATORS = frozenset(['**', '*', '/', '//', '+', '-']) +ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-']) +WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%']) WS_NEEDED_OPERATORS = frozenset([ - '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', - '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', - '%', '^', '&', '|', '=', '<', '>', '<<']) + '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>', + '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '=']) WHITESPACE = frozenset(' \t') SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT]) @@ -626,25 +626,16 @@ def missing_whitespace_around_operator(logical_line, tokens): Okay: hypot2 = x * x + y * y Okay: c = (a + b) * (a - b) Okay: foo(bar, key='word', *args, **kwargs) - Okay: baz(**kwargs) - Okay: negative = -1 - Okay: spam(-1) Okay: alpha[:-i] - Okay: if not -5 < x < +5:\n pass - Okay: lambda *args, **kw: (args, kw) - Okay: z = 2 ** 30 - Okay: x = x / 2 - 1 E225: i=i+1 E225: submitted +=1 - 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 + E227: c = a|b + E228: msg = fmt%(errno, errmsg) """ parens = 0 need_space = False @@ -674,8 +665,13 @@ def missing_whitespace_around_operator(logical_line, tokens): # 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") + code, optype = 'E226', 'arithmetic' + if prev_text == '%': + code, optype = 'E228', 'modulo' + elif prev_text not in ARITHMETIC_OP: + code, optype = 'E227', 'bitwise or shift' + yield (need_space[0], "%s missing whitespace " + "around %s operator" % (code, optype)) need_space = False elif token_type == tokenize.OP and prev_end is not None: if text == '=' and parens: diff --git a/testsuite/E22.py b/testsuite/E22.py index 36e028e..ba93d41 100644 --- a/testsuite/E22.py +++ b/testsuite/E22.py @@ -53,10 +53,14 @@ submitted+= 1 #: E225 c =-1 #: E225 +x = x /2 - 1 +#: E225 c = alpha -4 #: E225 c = alpha- 4 #: E225 +z = x **y +#: E225 z = (x + 1) **y #: E225 z = (x + 1)** y @@ -79,6 +83,9 @@ c = (a +b)*(a - b) #: E225 E226 c = (a+ b)*(a - b) #: + +#: E226 +z = 2**30 #: E226 c = (a+b) * (a-b) #: E226 @@ -86,13 +93,32 @@ norman = True+False #: E226 x = x*2 - 1 #: 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)) +#: E227 +_1kB = _1MB>>10 +#: E227 +_1MB = _1kB<<10 +#: E227 +a = b|c +#: E227 +b = c&a +#: E227 +c = b^a +#: E228 +a = b%c +#: E228 +msg = fmt%(errno, errmsg) +#: E228 +msg = "Error %d occured"%errno #: + #: Okay i = i + 1 submitted += 1 @@ -110,8 +136,10 @@ lambda a, b=h[:], c=0: (a, b, c) if not -5 < x < +5: print >>sys.stderr, "x is out of range." print >> sys.stdout, "x is an integer." +z = 2 ** 30 +x = x / 2 - 1 -if True: +if alpha[:-i]: *a, b = (1, 2, 3) |