summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Jemerov <yole@google.com>2014-07-24 20:06:44 +0200
committerDmitry Jemerov <yole@google.com>2014-07-24 20:06:44 +0200
commit103221e61a154aba6be0850a5ff142f3fd216b34 (patch)
tree55ddef3f46a378f966b2300e07cec511070772cb
parent4c5bf00cb613be617c7f48d3b2b82a1c7b895ac1 (diff)
downloadpep8-103221e61a154aba6be0850a5ff142f3fd216b34.tar.gz
Report W503 for line breaks before binary operators; issue #197. #ep14boat
-rwxr-xr-xpep8.py39
-rw-r--r--testsuite/E12.py6
2 files changed, 42 insertions, 3 deletions
diff --git a/pep8.py b/pep8.py
index b31a978..1dae086 100755
--- a/pep8.py
+++ b/pep8.py
@@ -922,6 +922,45 @@ def explicit_line_join(logical_line, tokens):
parens -= 1
+def break_around_binary_operator(logical_line, tokens):
+ r"""
+ Avoid breaks before binary operators.
+
+ The preferred place to break around a binary operator is after the
+ operator, not before it.
+
+ W503: (width == 0\n + height == 0)
+ W503: (width == 0\n and height == 0)
+
+ Okay: (width == 0 +\n height == 0)
+ Okay: foo(\n -x)
+ Okay: foo(x\n [])
+ Okay: x = '''\n''' + ''
+ Okay: foo(x,\n -y)
+ Okay: foo(x, # comment\n -y)
+ """
+ def is_binary_operator(token_type, text):
+ # The % character is strictly speaking a binary operator, but the
+ # common usage seems to be to put it next to the format parameters,
+ # after a line break.
+ return ((token_type == tokenize.OP or text == 'and' or text == 'or')
+ and text not in "()[]{},:.;@=%")
+
+ line_break = False
+ unary_context = True
+ for token_type, text, start, end, line in tokens:
+ if token_type == tokenize.COMMENT:
+ continue
+ if ('\n' in text or '\r' in text) and token_type != tokenize.STRING:
+ line_break = True
+ else:
+ if (is_binary_operator(token_type, text) and line_break
+ and not unary_context):
+ yield start, "W503 line break before binary operator"
+ unary_context = text in '([{,;'
+ line_break = False
+
+
def comparison_to_singleton(logical_line, noqa):
r"""Comparison to singletons should use "is" or "is not".
diff --git a/testsuite/E12.py b/testsuite/E12.py
index 6ebd44e..a995c95 100644
--- a/testsuite/E12.py
+++ b/testsuite/E12.py
@@ -323,14 +323,14 @@ if line_removed:
rv.update(d=('a', 'b', 'c'),
e=42)
#
-#: E127
+#: E127 W503
rv.update(d=('a' + 'b', 'c'),
e=42, f=42
+ 42)
-#: E127
+#: E127 W503
input1 = {'a': {'calc': 1 + 2}, 'b': 1
+ 42}
-#: E128
+#: E128 W503
rv.update(d=('a' + 'b', 'c'),
e=42, f=(42
+ 42))