Skip to content

Commit 7e10d9a

Browse files
committed
E225 Check for space around boolean operators
This was documented in the docstring for `missing_whitespace_around_operator` but not implemented, allowing e.g. `1and 0` to pass. Fixed by adding test case then modifying the check to search for the 'and' and 'or' operators as well.
1 parent e71908e commit 7e10d9a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pycodestyle.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
119119
FUNCTION_RETURN_ANNOTATION_OP = ['->'] if sys.version_info >= (3, 5) else []
120120
WS_NEEDED_OPERATORS = frozenset([
121121
'**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
122-
'%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='] +
122+
'%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '=',
123+
'and', 'or'] +
123124
FUNCTION_RETURN_ANNOTATION_OP)
124125
WHITESPACE = frozenset(' \t')
125126
NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE])
@@ -824,6 +825,7 @@ def missing_whitespace_around_operator(logical_line, tokens):
824825
E225: submitted +=1
825826
E225: x = x /2 - 1
826827
E225: z = x **y
828+
E225: z = 1and 1
827829
E226: c = (a+b) * (a-b)
828830
E226: hypot2 = x*x + y*y
829831
E227: c = a|b
@@ -833,6 +835,7 @@ def missing_whitespace_around_operator(logical_line, tokens):
833835
need_space = False
834836
prev_type = tokenize.OP
835837
prev_text = prev_end = None
838+
operator_types = (tokenize.OP, tokenize.NAME)
836839
for token_type, text, start, end, line in tokens:
837840
if token_type in SKIP_COMMENTS:
838841
continue
@@ -864,7 +867,7 @@ def missing_whitespace_around_operator(logical_line, tokens):
864867
yield (need_space[0], "%s missing whitespace "
865868
"around %s operator" % (code, optype))
866869
need_space = False
867-
elif token_type == tokenize.OP and prev_end is not None:
870+
elif token_type in operator_types and prev_end is not None:
868871
if text == '=' and parens:
869872
# Allow keyword args or defaults: foo(bar=None).
870873
pass

testsuite/E22.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
i=i+ 1
7777
#: E225 E225
7878
i=i +1
79+
#: E225
80+
i = 1and 1
81+
#: E225
82+
i = 1or 0
7983
#: E225 E226
8084
i=i+1
8185
#: E225 E226

0 commit comments

Comments
 (0)