Fix the E20 errors.

This commit is contained in:
Dave Halter
2017-06-23 09:37:02 +02:00
parent 97aebe4aa0
commit 2470f54baf
8 changed files with 99 additions and 21 deletions

View File

@@ -1,11 +1,14 @@
import re
from contextlib import contextmanager from contextlib import contextmanager
from parso.normalizer import Normalizer, Rule, NormalizerConfig from parso.normalizer import Normalizer, Rule, NormalizerConfig
_IMPORT_TYPES = ('import_name', 'import_from') _IMPORT_TYPES = ('import_name', 'import_from')
_SUITE_INTRODUCERS = ('classdef', 'funcdef', 'if_stmt', 'while_stmt', _SUITE_INTRODUCERS = ('classdef', 'funcdef', 'if_stmt', 'while_stmt',
'for_stmt', 'try_stmt', 'with_stmt') 'for_stmt', 'try_stmt', 'with_stmt')
_OPENING_BRACKETS = '([{' _OPENING_BRACKETS = '(', '[', '{'
_CLOSING_BRACKETS = ')', ']', '}'
_IMPLICIT_INDENTATION_TYPES = ('dictorsetmaker', 'argument') _IMPLICIT_INDENTATION_TYPES = ('dictorsetmaker', 'argument')
class CompressNormalizer(Normalizer): class CompressNormalizer(Normalizer):
@@ -163,6 +166,7 @@ def _is_magic_name(name):
class PEP8Normalizer(Normalizer): class PEP8Normalizer(Normalizer):
def __init__(self, config): def __init__(self, config):
super(PEP8Normalizer, self).__init__(config) super(PEP8Normalizer, self).__init__(config)
self._previous_leaf = None
self._last_indentation_level = 0 self._last_indentation_level = 0
self._on_newline = True self._on_newline = True
self._implicit_indentation_possible = False self._implicit_indentation_possible = False
@@ -334,7 +338,19 @@ class PEP8Normalizer(Normalizer):
self.add_issue(136, 'xxx', leaf) self.add_issue(136, 'xxx', leaf)
else: else:
self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf) self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf)
else:
spaces = info.indentation
if spaces:
if leaf in _CLOSING_BRACKETS:
message = "Whitespace before '%s'" % leaf.value
self.add_issue(202, message, info.indentation_part)
elif leaf in (',', ';') or leaf == ':' \
and leaf.parent.type not in ('subscript', 'subscriptlist'):
message = "Whitespace before '%s'" % leaf.value
self.add_issue(203, message, info.indentation_part)
elif self._previous_leaf in _OPENING_BRACKETS:
message = "Whitespace after '%s'" % leaf.value
self.add_issue(201, message, info.indentation_part)
first = True first = True
for comment in info.comments: for comment in info.comments:
@@ -412,6 +428,7 @@ class PEP8Normalizer(Normalizer):
if value == ':' and leaf.parent.type in _SUITE_INTRODUCERS: if value == ':' and leaf.parent.type in _SUITE_INTRODUCERS:
self._in_suite_introducer = False self._in_suite_introducer = False
self._previous_leaf = leaf
return value return value
@@ -426,7 +443,7 @@ class PEP8Normalizer(Normalizer):
self.add_issue(743, message % 'function', leaf) self.add_issue(743, message % 'function', leaf)
else: else:
self.add_issuadd_issue(741, message % 'variables', leaf) self.add_issuadd_issue(741, message % 'variables', leaf)
if leaf.value == ':': elif leaf.value == ':':
from parso.python.tree import Flow, Scope from parso.python.tree import Flow, Scope
if isinstance(leaf.parent, (Flow, Scope)) and leaf.parent.type != 'lambdef': if isinstance(leaf.parent, (Flow, Scope)) and leaf.parent.type != 'lambdef':
next_leaf = leaf.get_next_leaf() next_leaf = leaf.get_next_leaf()
@@ -435,12 +452,12 @@ class PEP8Normalizer(Normalizer):
self.add_issue(704, 'Multiple statements on one line (def)', next_leaf) self.add_issue(704, 'Multiple statements on one line (def)', next_leaf)
else: else:
self.add_issue(701, 'Multiple statements on one line (colon)', next_leaf) self.add_issue(701, 'Multiple statements on one line (colon)', next_leaf)
if leaf.value == ';': elif leaf.value == ';':
if leaf.get_next_leaf().type in ('newline', 'endmarker'): if leaf.get_next_leaf().type in ('newline', 'endmarker'):
self.add_issue(703, 'Statement ends with a semicolon', leaf) self.add_issue(703, 'Statement ends with a semicolon', leaf)
else: else:
self.add_issue(702, 'Multiple statements on one line (semicolon)', leaf) self.add_issue(702, 'Multiple statements on one line (semicolon)', leaf)
if leaf.value in ('==', '!='): elif leaf.value in ('==', '!='):
comparison = leaf.parent comparison = leaf.parent
index = comparison.children.index(leaf) index = comparison.children.index(leaf)
left = comparison.children[index - 1] left = comparison.children[index - 1]
@@ -455,13 +472,19 @@ class PEP8Normalizer(Normalizer):
message = "comparison to False/True should be 'if cond is True:' or 'if cond:'" message = "comparison to False/True should be 'if cond is True:' or 'if cond:'"
self.add_issue(712, message, leaf) self.add_issue(712, message, leaf)
break break
if leaf.value in ('in', 'is'): elif leaf.value in ('in', 'is'):
comparison = leaf.parent comparison = leaf.parent
if comparison.type == 'comparison' and comparison.parent.type == 'not_test': if comparison.type == 'comparison' and comparison.parent.type == 'not_test':
if leaf.value == 'in': if leaf.value == 'in':
self.add_issue(713, "test for membership should be 'not in'", leaf) self.add_issue(713, "test for membership should be 'not in'", leaf)
else: else:
self.add_issue(714, "test for object identity should be 'is not'", leaf) self.add_issue(714, "test for object identity should be 'is not'", leaf)
elif typ == 'string':
# Checking multiline strings
for i, line in enumerate(leaf.value.splitlines()[1:]):
indentation = re.match('[ \t]*', line).group(0)
start_pos = leaf.line + i, len(indentation)
# TODO check multiline indentation.
return leaf.value return leaf.value

View File

@@ -1,6 +1,6 @@
if x > 2: if x > 2:
#: E111:2 #: E111:2
print x print(x)
if True: if True:
#: E111:5 #: E111:5
print print

View File

@@ -60,14 +60,12 @@ rv.update(dict.fromkeys((
'?'), '?'),
"foo") "foo")
#: E126+1:10 E126+2:10
abricot = 3 + \ abricot = 3 + \
4 + \ 4 + \
5 + 6 5 + 6
print "hello", ( print "hello", (
"there", "there",
#: E131:5
# "john", # "john",
"dude") "dude")
part = set_mimetype(( part = set_mimetype((

View File

@@ -216,7 +216,7 @@ if (a == 2 or
"jkl mno"): "jkl mno"):
return True return True
#: E129+1:4 E127+2:9 #: E129+1:4
if (a == 2 or if (a == 2 or
b == """abc def ghi b == """abc def ghi
jkl mno"""): jkl mno"""):

View File

@@ -93,7 +93,7 @@ help = ur"print total number of errors " \
help = b"print total number of errors " \ help = b"print total number of errors " \
b"to standard error" b"to standard error"
#: E126+1:5 #: E122+1:5
help = br"print total number of errors " \ help = br"print total number of errors " \
br"to standard error" br"to standard error"

View File

@@ -134,11 +134,11 @@ my_list = [
4, 5, 6, 4, 5, 6,
#: E123:8 #: E123:8
] ]
#: E126+1:8 E126+2:8
abris = 3 + \ abris = 3 + \
4 + \ 4 + \
5 + 6 5 + 6
#: E126+1:8
fixed = re.sub(r'\t+', ' ', target[c::-1], 1)[::-1] + \ fixed = re.sub(r'\t+', ' ', target[c::-1], 1)[::-1] + \
target[c + 1:] target[c + 1:]
@@ -156,25 +156,26 @@ eat_a_dict_a_day({
#: E129+1:4 #: E129+1:4
if ( if (
x == ( x == (
#: E126:12
3 3
#: E129:4
) or ) or
y == 4): y == 4):
pass pass
#: E129+1:4 E129+4:4 #: E129+1:4 E121+2:8 E129+3:4
if ( if (
x == ( x == (
3 3
) or ) or
x == ( x == (
#: E126:12 # This one has correct indentation.
3 3
#: E129:4
) or ) or
y == 4): y == 4):
pass pass
troublesome_hash = { troublesome_hash = {
"hash": "value", "hash": "value",
#: E131+1:8 #: E135+1:8
"long": "the quick brown fox jumps over the lazy dog before doing a " "long": "the quick brown fox jumps over the lazy dog before doing a "
"somersault", "somersault",
} }

View File

@@ -58,10 +58,13 @@ rv.update(d=('a', 'b', 'c'),
#: E127:13 #: E127:13
e=42) e=42)
#: E127+2:17 #: E135+2:17
rv.update(d=('a' + 'b', 'c'), rv.update(d=('a' + 'b', 'c'),
e=42, f=42 e=42, f=42
+ 42) + 42)
rv.update(d=('a' + 'b', 'c'),
e=42, f=42
+ 42)
#: E127+1:26 #: E127+1:26
input1 = {'a': {'calc': 1 + 2}, 'b': 1 input1 = {'a': {'calc': 1 + 2}, 'b': 1
+ 42} + 42}
@@ -102,7 +105,7 @@ print dedent(
mkdir -p ./{build}/ mkdir -p ./{build}/
mv ./build/ ./{build}/%(revision)s/ mv ./build/ ./{build}/%(revision)s/
'''.format( '''.format(
#: E121:4 E121+1:5 E123+2:0 #: E121:4 E123+2:0
build='build', build='build',
# more stuff # more stuff
) )
@@ -112,5 +115,5 @@ if True:\
print(True) print(True)
#: E128+1 #: E128+1
print(a foobar(a
, end=' ') , end=' ')

View File

@@ -0,0 +1,53 @@
#: E201:5
spam( ham[1], {eggs: 2})
#: E201:9
spam(ham[ 1], {eggs: 2})
#: E201:14
spam(ham[1], { eggs: 2})
# Okay
spam(ham[1], {eggs: 2})
#: E202:22
spam(ham[1], {eggs: 2} )
#: E202:21
spam(ham[1], {eggs: 2 })
#: E202:10
spam(ham[1 ], {eggs: 2})
# Okay
spam(ham[1], {eggs: 2})
result = func(
arg1='some value',
arg2='another value',
)
result = func(
arg1='some value',
arg2='another value'
)
result = [
item for item in items
if item > 5
]
#: E203:9
if x == 4 :
print x, y
x, y = y, x
if x == 4:
#: E203:12 E702:13
a = x, y ; x, y = y, x
if x == 4:
print x, y
#: E203:12
x, y = y , x
# Okay
if x == 4:
print x, y
x, y = y, x
a[b1, :] == a[b1, ...]
a[b1, :1] == 3
b = a[:, b1]