Skip to content

Commit 72269dd

Browse files
committed
Report E227 or E228 instead of E225 for whitespace around bitwise, shift or modulo operators; issue #166
1 parent 2a82c7e commit 72269dd

4 files changed

Lines changed: 54 additions & 20 deletions

File tree

CHANGES.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Changelog
55
1.4.x (unreleased)
66
------------------
77

8+
* Report E227 or E228 instead of E225 for whitespace around bitwise, shift
9+
or modulo operators. (Issue #166)
10+
11+
* Change the message for E226 to make clear that it is about arithmetic
12+
operators.
13+
814
* Fix regression with the ``--diff`` option. (Issue #169)
915

1016
* Fix the ``TestReport`` class to print the unexpected warnings and
@@ -63,7 +69,7 @@ Changelog
6369
1.4 (2012-12-22)
6470
----------------
6571

66-
* Report E226 instead of E225 for optional white space around common
72+
* Report E226 instead of E225 for optional whitespace around common
6773
operators (``*``, ``**``, ``/``, ``+`` and ``-``). This new error
6874
code is ignored in the default configuration because PEP 8 recommends
6975
to "use your own judgement". (Issue #96)

docs/intro.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ This is the current list of error and warning codes:
230230
+----------+----------------------------------------------------------------------+
231231
| E225 | missing whitespace around operator |
232232
+----------+----------------------------------------------------------------------+
233-
| E226 (*) | missing optional whitespace around operator |
233+
| E226 (*) | missing whitespace around arithmetic operator |
234+
+----------+----------------------------------------------------------------------+
235+
| E227 | missing whitespace around bitwise or shift operator |
236+
+----------+----------------------------------------------------------------------+
237+
| E228 | missing whitespace around modulo operator |
234238
+----------+----------------------------------------------------------------------+
235239
+----------+----------------------------------------------------------------------+
236240
| E231 | missing whitespace after ',' |

pep8.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@
8181
SINGLETONS = frozenset(['False', 'None', 'True'])
8282
KEYWORDS = frozenset(keyword.kwlist + ['print']) - SINGLETONS
8383
UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
84-
WS_OPTIONAL_OPERATORS = frozenset(['**', '*', '/', '//', '+', '-'])
84+
ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-'])
85+
WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%'])
8586
WS_NEEDED_OPERATORS = frozenset([
86-
'**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>',
87-
'%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=',
88-
'%', '^', '&', '|', '=', '<', '>', '<<'])
87+
'**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
88+
'%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='])
8989
WHITESPACE = frozenset(' \t')
9090
SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE,
9191
tokenize.INDENT, tokenize.DEDENT])
@@ -626,25 +626,16 @@ def missing_whitespace_around_operator(logical_line, tokens):
626626
Okay: hypot2 = x * x + y * y
627627
Okay: c = (a + b) * (a - b)
628628
Okay: foo(bar, key='word', *args, **kwargs)
629-
Okay: baz(**kwargs)
630-
Okay: negative = -1
631-
Okay: spam(-1)
632629
Okay: alpha[:-i]
633-
Okay: if not -5 < x < +5:\n pass
634-
Okay: lambda *args, **kw: (args, kw)
635-
Okay: z = 2 ** 30
636-
Okay: x = x / 2 - 1
637630
638631
E225: i=i+1
639632
E225: submitted +=1
640-
E225: c = alpha -4
641633
E225: x = x /2 - 1
642634
E225: z = x **y
643635
E226: c = (a+b) * (a-b)
644-
E226: z = 2**30
645-
E226: x = x*2 - 1
646-
E226: x = x/2 - 1
647636
E226: hypot2 = x*x + y*y
637+
E227: c = a|b
638+
E228: msg = fmt%(errno, errmsg)
648639
"""
649640
parens = 0
650641
need_space = False
@@ -674,8 +665,13 @@ def missing_whitespace_around_operator(logical_line, tokens):
674665
# A needed trailing space was not found
675666
yield prev_end, "E225 missing whitespace around operator"
676667
else:
677-
yield (need_space[0],
678-
"E226 missing optional whitespace around operator")
668+
code, optype = 'E226', 'arithmetic'
669+
if prev_text == '%':
670+
code, optype = 'E228', 'modulo'
671+
elif prev_text not in ARITHMETIC_OP:
672+
code, optype = 'E227', 'bitwise or shift'
673+
yield (need_space[0], "%s missing whitespace "
674+
"around %s operator" % (code, optype))
679675
need_space = False
680676
elif token_type == tokenize.OP and prev_end is not None:
681677
if text == '=' and parens:

testsuite/E22.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@
5353
#: E225
5454
c =-1
5555
#: E225
56+
x = x /2 - 1
57+
#: E225
5658
c = alpha -4
5759
#: E225
5860
c = alpha- 4
5961
#: E225
62+
z = x **y
63+
#: E225
6064
z = (x + 1) **y
6165
#: E225
6266
z = (x + 1)** y
@@ -79,20 +83,42 @@
7983
#: E225 E226
8084
c = (a+ b)*(a - b)
8185
#:
86+
87+
#: E226
88+
z = 2**30
8289
#: E226
8390
c = (a+b) * (a-b)
8491
#: E226
8592
norman = True+False
8693
#: E226
8794
x = x*2 - 1
8895
#: E226
96+
x = x/2 - 1
97+
#: E226
8998
hypot2 = x*x + y*y
9099
#: E226
91100
c = (a + b)*(a - b)
92101
#: E226
93102
def squares(n):
94103
return (i**2 for i in range(n))
104+
#: E227
105+
_1kB = _1MB>>10
106+
#: E227
107+
_1MB = _1kB<<10
108+
#: E227
109+
a = b|c
110+
#: E227
111+
b = c&a
112+
#: E227
113+
c = b^a
114+
#: E228
115+
a = b%c
116+
#: E228
117+
msg = fmt%(errno, errmsg)
118+
#: E228
119+
msg = "Error %d occured"%errno
95120
#:
121+
96122
#: Okay
97123
i = i + 1
98124
submitted += 1
@@ -110,8 +136,10 @@ def squares(n):
110136
if not -5 < x < +5:
111137
print >>sys.stderr, "x is out of range."
112138
print >> sys.stdout, "x is an integer."
139+
z = 2 ** 30
140+
x = x / 2 - 1
113141

114-
if True:
142+
if alpha[:-i]:
115143
*a, b = (1, 2, 3)
116144

117145

0 commit comments

Comments
 (0)