summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2012-12-19 01:48:14 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2012-12-19 01:48:14 +0100
commit8c9ac17a72da317c92ecf4df6c309307aeabb8bb (patch)
treed36baed068a4493beaac3df13d7e6204d4c1839e
parent1a5dfd0b3df6fe36d10d8199f62c7e03abc52a8a (diff)
parent515eb192a37c965d9a80cd10e152ba9a70030b50 (diff)
downloadpep8-8c9ac17a72da317c92ecf4df6c309307aeabb8bb.tar.gz
Merge branch 'local'
-rw-r--r--CHANGES.txt2
-rwxr-xr-xpep8.py42
-rw-r--r--testsuite/E21.py3
-rw-r--r--testsuite/E22.py15
4 files changed, 42 insertions, 20 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index d70aab4..f089e74 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -5,7 +5,7 @@ Changelog
1.x (UNRELEASED)
----------------
-* ...
+* Relax E225 for high priority operators (``*``, ``**``, ``/``). (Issue #96)
1.3.4 (2012-12-18)
diff --git a/pep8.py b/pep8.py
index bc70e53..2dcae3e 100755
--- a/pep8.py
+++ b/pep8.py
@@ -127,12 +127,13 @@ REPORT_FORMAT = {
SINGLETONS = frozenset(['False', 'None', 'True'])
KEYWORDS = frozenset(keyword.kwlist + ['print']) - SINGLETONS
-BINARY_OPERATORS = frozenset([
+WS_NEEDED_OPERATORS = frozenset([
'**=', '*=', '+=', '-=', '!=', '<>',
'%=', '^=', '&=', '|=', '==', '/=', '//=', '<=', '>=', '<<=', '>>=',
- '%', '^', '&', '|', '=', '/', '//', '<', '>', '<<'])
+ '%', '^', '&', '|', '=', '<', '>', '<<'])
+WS_OPTIONAL_OPERATORS = frozenset([
+ '**', '*', '/', '//'])
UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
-OPERATORS = BINARY_OPERATORS | UNARY_OPERATORS
WHITESPACE = frozenset(' \t')
SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE,
tokenize.INDENT, tokenize.DEDENT])
@@ -675,13 +676,18 @@ def missing_whitespace_around_operator(logical_line, tokens):
Okay: alpha[:-i]
Okay: if not -5 < x < +5:\n pass
Okay: lambda *args, **kw: (args, kw)
+ Okay: z = 2**30
+ Okay: z = 2 ** 30
+ Okay: x = x*2 - 1
+ Okay: x = x / 2 - 1
+ Okay: x = x/2 - 1
+ Okay: hypot2 = x*x + y*y
E225: i=i+1
E225: submitted +=1
- E225: x = x*2 - 1
- E225: hypot2 = x*x + y*y
E225: c = (a+b) * (a-b)
E225: c = alpha -4
+ E225: x = x /2 - 1
E225: z = x **y
"""
parens = 0
@@ -698,32 +704,50 @@ def missing_whitespace_around_operator(logical_line, tokens):
parens -= 1
if need_space:
if start != prev_end:
+ # Found a needed space
need_space = False
elif text == '>' and prev_text in ('<', '-'):
# Tolerate the "<>" operator, even if running Python 3
# Deal with Python 3's annotated return value "->"
pass
else:
+ # A needed trailing space was not found
yield prev_end, "E225 missing whitespace around operator"
need_space = False
elif token_type == tokenize.OP and prev_end is not None:
if text == '=' and parens:
# Allow keyword args or defaults: foo(bar=None).
pass
- elif text in BINARY_OPERATORS:
+ elif text in WS_NEEDED_OPERATORS:
need_space = True
elif text in UNARY_OPERATORS:
+ # Check if the operator is being used as a binary operator
# Allow unary operators: -123, -x, +1.
# Allow argument unpacking: foo(*args, **kwargs).
+ binary_usage = False
if prev_type == tokenize.OP:
if prev_text in '}])':
- need_space = True
+ binary_usage = True
elif prev_type == tokenize.NAME:
if prev_text not in KEYWORDS:
- need_space = True
+ binary_usage = True
elif prev_type not in SKIP_TOKENS:
- need_space = True
+ binary_usage = True
+
+ if binary_usage:
+ if text in WS_OPTIONAL_OPERATORS:
+ # Surrounding space is optional
+ # Ensure trailing space matches opening space
+ need_space = start != prev_end
+ else:
+ need_space = True
+ elif text in WS_OPTIONAL_OPERATORS:
+ # Surrounding space is optional
+ # Ensure trailing space matches opening space
+ need_space = start != prev_end
+
if need_space and start == prev_end:
+ # A needed opening space was not found
yield prev_end, "E225 missing whitespace around operator"
need_space = False
prev_type = token_type
diff --git a/testsuite/E21.py b/testsuite/E21.py
index 2280caf..e830895 100644
--- a/testsuite/E21.py
+++ b/testsuite/E21.py
@@ -4,9 +4,6 @@ spam (1)
dict ['key'] = list [index]
#: E211
dict['key'] ['subkey'] = list[index]
-#: E225
-def squares(n):
- return (i**2 for i in range(n))
#: Okay
spam(1)
dict['key'] = list[index]
diff --git a/testsuite/E22.py b/testsuite/E22.py
index a6916ff..96393d0 100644
--- a/testsuite/E22.py
+++ b/testsuite/E22.py
@@ -51,14 +51,8 @@ i=i+1
#: E225
submitted +=1
#: E225
-x = x*2 - 1
-#: E225
-hypot2 = x*x + y*y
-#: E225
c = (a+b) * (a-b)
#: E225
-c = (a + b)*(a - b)
-#: E225
c = (a +b)*(a - b)
#: E225
c =-1
@@ -69,15 +63,18 @@ z = (x + 1) **y
#: E225
norman = True+False
#: E225
-_1MB = 2 ** 20
_1kB = _1MB >>10
#:
#: Okay
i = i + 1
submitted += 1
x = x * 2 - 1
+x = x*2 - 1
hypot2 = x * x + y * y
+hypot2 = x*x + y*y
c = (a + b) * (a - b)
+c = (a + b)*(a - b)
+_1MB = 2 ** 20
foo(bar, key='word', *args, **kwargs)
baz(**kwargs)
negative = -1
@@ -91,4 +88,8 @@ print >> sys.stdout, "x is an integer."
if True:
*a, b = (1, 2, 3)
+
+
+def squares(n):
+ return (i**2 for i in range(n))
#: