[Python-modules-commits] [autopep8] 02/03: New upstream version 1.3.3
Sylvestre Ledru
sylvestre at moszumanska.debian.org
Tue Oct 24 13:24:18 UTC 2017
This is an automated email from the git hooks/post-receive script.
sylvestre pushed a commit to branch master
in repository autopep8.
commit e5bc10ed13ccb683523e091a9dfc4fd3aa6be25f
Author: Sylvestre Ledru <sylvestre at debian.org>
Date: Tue Oct 24 15:23:46 2017 +0200
New upstream version 1.3.3
---
autopep8.py | 111 +++++++++++++++++++++++++++++++++++++++++---------
test/suite/out/E70.py | 3 +-
test/test_autopep8.py | 71 ++++++++++++++++++++++++++++++++
3 files changed, 164 insertions(+), 21 deletions(-)
diff --git a/autopep8.py b/autopep8.py
index fb32e12..b208f96 100755
--- a/autopep8.py
+++ b/autopep8.py
@@ -66,7 +66,7 @@ except NameError:
unicode = str
-__version__ = '1.3.2'
+__version__ = '1.3.3'
CR = '\r'
@@ -77,6 +77,7 @@ CRLF = '\r\n'
PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[23]?\b\s*$')
LAMBDA_REGEX = re.compile(r'([\w.]+)\s=\slambda\s*([\(\)\w,\s.]*):')
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+([^][)(}{]+)\s+(in|is)\s')
+COMPARE_NEGATIVE_REGEX_THROUGH = re.compile(r'\b(not\s+in)\s')
BARE_EXCEPT_REGEX = re.compile(r'except\s*:')
STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)\s.*\):')
@@ -805,6 +806,7 @@ class FixPEP8(object):
offset -= 1
offset += 1
self.source[offset] = cr + self.source[offset]
+ return [1 + offset] # Line indexed at 1.
def fix_e401(self, result):
"""Put imports on separate lines."""
@@ -885,12 +887,15 @@ class FixPEP8(object):
if cache_entry in self.long_line_ignore_cache:
return []
- if self.options.aggressive and target.lstrip().startswith('#'):
- # Wrap commented lines.
- return shorten_comment(
- line=target,
- max_line_length=self.options.max_line_length,
- last_comment=not next_line.lstrip().startswith('#'))
+ if target.lstrip().startswith('#'):
+ if self.options.aggressive:
+ # Wrap commented lines.
+ return shorten_comment(
+ line=target,
+ max_line_length=self.options.max_line_length,
+ last_comment=not next_line.lstrip().startswith('#'))
+ else:
+ return []
fixed = get_fixed_long_line(
target=target,
@@ -935,7 +940,7 @@ class FixPEP8(object):
# Avoid applying this when indented.
# https://docs.python.org/reference/compound_stmts.html
for line in logical_lines:
- if ':' in line:
+ if ':' in line and STARTSWITH_DEF_REGEX.match(line):
return []
line_index = result['line'] - 1
@@ -1043,16 +1048,35 @@ class FixPEP8(object):
def fix_e713(self, result):
"""Fix (trivial case of) non-membership check."""
- (line_index, _, target) = get_index_offset_contents(result,
- self.source)
+ (line_index, offset, target) = get_index_offset_contents(result,
+ self.source)
+ # to convert once 'not in' -> 'in'
+ before_target = target[:offset]
+ target = target[offset:]
+ match_notin = COMPARE_NEGATIVE_REGEX_THROUGH.search(target)
+ notin_pos_start, notin_pos_end = 0, 0
+ if match_notin:
+ notin_pos_start = match_notin.start(1)
+ notin_pos_end = match_notin.end()
+ target = '{0}{1} {2}'.format(
+ target[:notin_pos_start], 'in', target[notin_pos_end:])
+
+ # fix 'not in'
match = COMPARE_NEGATIVE_REGEX.search(target)
if match:
if match.group(3) == 'in':
pos_start = match.start(1)
- self.source[line_index] = '{0}{1} {2} {3} {4}'.format(
+ new_target = '{5}{0}{1} {2} {3} {4}'.format(
target[:pos_start], match.group(2), match.group(1),
- match.group(3), target[match.end():])
+ match.group(3), target[match.end():], before_target)
+ if match_notin:
+ # revert 'in' -> 'not in'
+ pos_start = notin_pos_start + offset
+ pos_end = notin_pos_end + offset - 4 # len('not ')
+ new_target = '{0}{1} {2}'.format(
+ new_target[:pos_start], 'not in', new_target[pos_end:])
+ self.source[line_index] = new_target
def fix_e714(self, result):
"""Fix object identity should be 'is not' case."""
@@ -1117,15 +1141,51 @@ class FixPEP8(object):
return
if not _is_binary_operator(ts[0][0], one_string_token):
return
+ # find comment
+ comment_index = None
+ for i in range(5):
+ # NOTE: try to parse code in 5 times
+ if (line_index - i) < 0:
+ break
+ from_index = line_index - i - 1
+ to_index = line_index + 1
+ try:
+ ts = generate_tokens("".join(self.source[from_index:to_index]))
+ except Exception:
+ continue
+ newline_count = 0
+ newline_index = []
+ for i, t in enumerate(ts):
+ if t[0] in (tokenize.NEWLINE, tokenize.NL):
+ newline_index.append(i)
+ newline_count += 1
+ if newline_count > 2:
+ tts = ts[newline_index[-3]:]
+ else:
+ tts = ts
+ old = None
+ for t in tts:
+ if tokenize.COMMENT == t[0]:
+ if old is None:
+ comment_index = 0
+ else:
+ comment_index = old[3][1]
+ break
+ old = t
+ break
i = target.index(one_string_token)
self.source[line_index] = '{0}{1}'.format(
target[:i], target[i + len(one_string_token):])
nl = find_newline(self.source[line_index - 1:line_index])
before_line = self.source[line_index - 1]
bl = before_line.index(nl)
- self.source[line_index - 1] = '{0} {1}{2}'.format(
- before_line[:bl], one_string_token,
- before_line[bl:])
+ if comment_index:
+ self.source[line_index - 1] = '{0} {1} {2}'.format(
+ before_line[:comment_index], one_string_token,
+ before_line[comment_index + 1:])
+ else:
+ self.source[line_index - 1] = '{0} {1}{2}'.format(
+ before_line[:bl], one_string_token, before_line[bl:])
def get_index_offset_contents(result, source):
@@ -1148,6 +1208,7 @@ def get_fixed_long_line(target, previous_line, original,
indent = _get_indentation(target)
source = target[len(indent):]
assert source.lstrip() == source
+ assert not target.lstrip().startswith('#')
# Check for partial multiline.
tokens = list(generate_tokens(source))
@@ -1378,16 +1439,21 @@ def refactor(source, fixer_names, ignore=None, filename=''):
return new_text
-def code_to_2to3(select, ignore):
+def code_to_2to3(select, ignore, where='', verbose=False):
fixes = set()
for code, fix in CODE_TO_2TO3.items():
if code_match(code, select=select, ignore=ignore):
+ if verbose:
+ print('---> Applying {0} fix for {1}'.format(where,
+ code.upper()),
+ file=sys.stderr)
fixes |= set(fix)
return fixes
def fix_2to3(source,
- aggressive=True, select=None, ignore=None, filename=''):
+ aggressive=True, select=None, ignore=None, filename='',
+ where='global', verbose=False):
"""Fix various deprecated code (via lib2to3)."""
if not aggressive:
return source
@@ -1397,7 +1463,9 @@ def fix_2to3(source,
return refactor(source,
code_to_2to3(select=select,
- ignore=ignore),
+ ignore=ignore,
+ where=where,
+ verbose=verbose),
filename=filename)
@@ -1495,7 +1563,8 @@ def _priority_key(pep8_result):
lowest_priority = [
# We need to shorten lines last since the logical fixer can get in a
# loop, which causes us to exit early.
- 'e501'
+ 'e501',
+ 'w503'
]
key = pep8_result['id'].lower()
try:
@@ -3176,7 +3245,9 @@ def apply_global_fixes(source, options, where='global', filename=''):
aggressive=options.aggressive,
select=options.select,
ignore=options.ignore,
- filename=filename)
+ filename=filename,
+ where=where,
+ verbose=options.verbose)
return source
diff --git a/test/suite/out/E70.py b/test/suite/out/E70.py
index c320321..a9d9276 100644
--- a/test/suite/out/E70.py
+++ b/test/suite/out/E70.py
@@ -13,5 +13,6 @@ bdist_egg.write_safety_flag(cmd.egg_info, safe)
#: E703
import shlex
#: E702 E703
-del a[:]; a.append(42);
+del a[:]
+a.append(42)
#:
diff --git a/test/test_autopep8.py b/test/test_autopep8.py
index 28e7153..e4381e8 100755
--- a/test/test_autopep8.py
+++ b/test/test_autopep8.py
@@ -3199,6 +3199,16 @@ class Migration(SchemaMigration):
with autopep8_context(line, options=['-aa']) as result:
self.assertEqual(fixed, result)
+ def test_e501_shorten_comment_without_aggressive(self):
+ """Do nothing without aggressive."""
+ line = """\
+def foo():
+ pass
+# --------- ----------------------------------------------------------------------
+"""
+ with autopep8_context(line) as result:
+ self.assertEqual(line, result)
+
def test_e501_with_aggressive_and_escaped_newline(self):
line = """\
if True or \\
@@ -3719,6 +3729,22 @@ raise IOError('abc '
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
+ def test_e702_with_dict_semicolon(self):
+ line = """\
+MY_CONST = [
+ {'A': 1},
+ {'B': 2}
+];
+"""
+ fixed = """\
+MY_CONST = [
+ {'A': 1},
+ {'B': 2}
+]
+"""
+ with autopep8_context(line) as result:
+ self.assertEqual(fixed, result)
+
def test_e703_with_inline_comment(self):
line = 'a = 5; # inline comment\n'
fixed = 'a = 5 # inline comment\n'
@@ -3841,6 +3867,27 @@ if role not in ("domaincontroller_master",
options=['-aa', '--select=E713']) as result:
self.assertEqual(fixed, result)
+ def test_e713_chain(self):
+ line = 'if "@" not in x or not "/" in y:\n pass\n'
+ fixed = 'if "@" not in x or "/" not in y:\n pass\n'
+ with autopep8_context(line,
+ options=['-aa', '--select=E713']) as result:
+ self.assertEqual(fixed, result)
+
+ def test_e713_chain2(self):
+ line = 'if "@" not in x or "[" not in x or not "/" in y:\n pass\n'
+ fixed = 'if "@" not in x or "[" not in x or "/" not in y:\n pass\n'
+ with autopep8_context(line,
+ options=['-aa', '--select=E713']) as result:
+ self.assertEqual(fixed, result)
+
+ def test_e713_chain3(self):
+ line = 'if not "@" in x or "[" not in x or not "/" in y:\n pass\n'
+ fixed = 'if "@" not in x or "[" not in x or "/" not in y:\n pass\n'
+ with autopep8_context(line,
+ options=['-aa', '--select=E713']) as result:
+ self.assertEqual(fixed, result)
+
def test_e714(self):
line = 'if not x is y:\n pass\n'
fixed = 'if x is not y:\n pass\n'
@@ -4080,6 +4127,30 @@ else:
with autopep8_context(line, options=['-aaa']) as result:
self.assertEqual(fixed, result)
+ def test_w503_with_comment(self):
+ line = '(width == 0 # this is comment\n + height == 0)\n'
+ fixed = '(width == 0 + # this is comment\n height == 0)\n'
+ with autopep8_context(line, options=['-aaa']) as result:
+ self.assertEqual(fixed, result)
+
+ def test_w503_with_comment_double(self):
+ line = """\
+(
+ 1111 # C1
+ and 22222222 # C2
+ and 333333333333 # C3
+)
+"""
+ fixed = """\
+(
+ 1111 and # C1
+ 22222222 and # C2
+ 333333333333 # C3
+)
+"""
+ with autopep8_context(line, options=['-aaa']) as result:
+ self.assertEqual(fixed, result)
+
def test_w601(self):
line = 'a = {0: 1}\na.has_key(0)\n'
fixed = 'a = {0: 1}\n0 in a\n'
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/autopep8.git
More information about the Python-modules-commits
mailing list