From 7e10d9a95e34aa7c600f5100ba64e7710c2688f1 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 27 Feb 2019 20:05:59 +0200 Subject: 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. --- pycodestyle.py | 7 +++++-- testsuite/E22.py | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 4a0d17b..b2285ba 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -119,7 +119,8 @@ WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%']) FUNCTION_RETURN_ANNOTATION_OP = ['->'] if sys.version_info >= (3, 5) else [] WS_NEEDED_OPERATORS = frozenset([ '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>', - '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='] + + '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '=', + 'and', 'or'] + FUNCTION_RETURN_ANNOTATION_OP) WHITESPACE = frozenset(' \t') NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE]) @@ -824,6 +825,7 @@ def missing_whitespace_around_operator(logical_line, tokens): E225: submitted +=1 E225: x = x /2 - 1 E225: z = x **y + E225: z = 1and 1 E226: c = (a+b) * (a-b) E226: hypot2 = x*x + y*y E227: c = a|b @@ -833,6 +835,7 @@ def missing_whitespace_around_operator(logical_line, tokens): need_space = False prev_type = tokenize.OP prev_text = prev_end = None + operator_types = (tokenize.OP, tokenize.NAME) for token_type, text, start, end, line in tokens: if token_type in SKIP_COMMENTS: continue @@ -864,7 +867,7 @@ def missing_whitespace_around_operator(logical_line, tokens): 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: + elif token_type in operator_types and prev_end is not None: if text == '=' and parens: # Allow keyword args or defaults: foo(bar=None). pass diff --git a/testsuite/E22.py b/testsuite/E22.py index ee9dc74..351e976 100644 --- a/testsuite/E22.py +++ b/testsuite/E22.py @@ -76,6 +76,10 @@ _1kB = _1MB>> 10 i=i+ 1 #: E225 E225 i=i +1 +#: E225 +i = 1and 1 +#: E225 +i = 1or 0 #: E225 E226 i=i+1 #: E225 E226 -- cgit v1.2.1