Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E225 Check for space around boolean operators #847

Merged
merged 2 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
FUNCTION_RETURN_ANNOTATION_OP = ['->'] if sys.version_info >= (3, 5) else []
WS_NEEDED_OPERATORS = frozenset([
'**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
'%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='] +
'%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '=',
'and', 'in', 'is', 'or'] +
FUNCTION_RETURN_ANNOTATION_OP)
WHITESPACE = frozenset(' \t')
NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE])
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions testsuite/E22.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
i=i+ 1
#: E225 E225
i=i +1
#: E225
i = 1and 1
#: E225
i = 1or 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a few more:

1is 1
1not in []
1in []

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support for 'is' and 'not', updating for the two-word 'not in' is beyond me right now (07:30 after a long haul flight...)

#: E225
1is 1
#: E225
1in []
#: E225 E226
i=i+1
#: E225 E226
Expand Down