Skip to content

Commit

Permalink
Merge pull request #643 from hhatto/refactor/remove-py2code
Browse files Browse the repository at this point in the history
refactoring: remove py2 specific code
  • Loading branch information
hhatto committed Aug 6, 2022
2 parents 16a4d66 + 48b4bbf commit e7b4944
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 69 deletions.
85 changes: 37 additions & 48 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,12 @@ class documentation for more information.
import tokenize
import warnings
import ast
try:
from configparser import ConfigParser as SafeConfigParser
from configparser import Error
except ImportError:
from ConfigParser import SafeConfigParser
from ConfigParser import Error
from configparser import ConfigParser as SafeConfigParser, Error

import pycodestyle
from pycodestyle import STARTSWITH_INDENT_STATEMENT_REGEX


try:
unicode
except NameError:
unicode = str


__version__ = '1.6.0'


Expand Down Expand Up @@ -331,7 +320,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
elif visual_indent is True:
# Visual indent is verified.
indent[depth] = start[1]
elif visual_indent in (text, unicode):
elif visual_indent in (text, str):
# Ignore token lined up with matching one from a previous line.
pass
else:
Expand Down Expand Up @@ -363,7 +352,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
# Deal with implicit string concatenation.
elif (token_type in (tokenize.STRING, tokenize.COMMENT) or
text in ('u', 'ur', 'b', 'br')):
indent_chances[start[1]] = unicode
indent_chances[start[1]] = str
# Special case for the "if" statement because len("if (") is equal to
# 4.
elif not indent_chances and not row and not depth and text == 'if':
Expand Down Expand Up @@ -1815,7 +1804,7 @@ def find_newline(source):
Input is a list of lines.
"""
assert not isinstance(source, unicode)
assert not isinstance(source, str)

counter = collections.defaultdict(int)
for line in source:
Expand Down Expand Up @@ -2123,9 +2112,9 @@ def add_space_if_needed(self, curr_text, equal=False):
):
return

prev_text = unicode(self._prev_item)
prev_text = str(self._prev_item)
prev_prev_text = (
unicode(self._prev_prev_item) if self._prev_prev_item else '')
str(self._prev_prev_item) if self._prev_prev_item else '')

if (
# The previous item was a keyword or identifier and the current
Expand Down Expand Up @@ -2209,7 +2198,7 @@ def _add_item(self, item, indent_amt):
self._lines.append(self._LineBreak())
self._lines.append(self._Indent(indent_amt))

item_text = unicode(item)
item_text = str(item)
if self._lines and self._bracket_depth:
# Adding the item into a container.
self._prevent_default_initializer_splitting(item, indent_amt)
Expand Down Expand Up @@ -2241,19 +2230,19 @@ def _add_container(self, container, indent_amt, break_after_open_bracket):
actual_indent = indent_amt + 1

if (
unicode(self._prev_item) != '=' and
str(self._prev_item) != '=' and
not self.line_empty() and
not self.fits_on_current_line(
container.size + self._bracket_depth + 2)
):

if unicode(container)[0] == '(' and self._prev_item.is_name:
if str(container)[0] == '(' and self._prev_item.is_name:
# Don't split before the opening bracket of a call.
break_after_open_bracket = True
actual_indent = indent_amt + 4
elif (
break_after_open_bracket or
unicode(self._prev_item) not in '([{'
str(self._prev_item) not in '([{'
):
# If the container doesn't fit on the current line and the
# current line isn't empty, place the container on the next
Expand Down Expand Up @@ -2284,14 +2273,14 @@ def _prevent_default_initializer_splitting(self, item, indent_amt):
break/indent before it if needed.
"""
if unicode(item) == '=':
if str(item) == '=':
# This is the assignment in the initializer. Just remove spaces for
# now.
self._delete_whitespace()
return

if (not self._prev_item or not self._prev_prev_item or
unicode(self._prev_item) != '='):
str(self._prev_item) != '='):
return

self._delete_whitespace()
Expand Down Expand Up @@ -2353,8 +2342,8 @@ def _enforce_space(self, item):
if not self._prev_item:
return

item_text = unicode(item)
prev_text = unicode(self._prev_item)
item_text = str(item)
prev_text = str(self._prev_item)

# Prefer a space around a '.' in an import statement, and between the
# 'import' and '('.
Expand Down Expand Up @@ -2410,13 +2399,13 @@ def reflow(
not reflowed_lines.line_empty() and
not self.is_colon and
not (prev_item and prev_item.is_name and
unicode(self) == '(')
str(self) == '(')
):
# Start a new line if there is already something on the line and
# adding this atom would make it go over the max line length.
reflowed_lines.add_line_break(continued_indent)
else:
reflowed_lines.add_space_if_needed(unicode(self))
reflowed_lines.add_space_if_needed(str(self))

reflowed_lines.add(self, len(continued_indent),
break_after_open_bracket)
Expand Down Expand Up @@ -2470,7 +2459,7 @@ def __repr__(self):
elif item.is_colon:
string += ': '
else:
item_string = unicode(item)
item_string = str(item)
if (
string and
(last_was_keyword or
Expand Down Expand Up @@ -2503,7 +2492,7 @@ def reflow(self, reflowed_lines, continued_indent,
self._get_extent(index),
is_list_comp_or_if_expr=is_list_comp_or_if_expr,
next_is_dot=(next_item and
unicode(next_item) == '.'))
str(next_item) == '.'))
if last_was_container and item.is_comma:
reflowed_lines.add_line_break(continued_indent)
last_was_container = False
Expand All @@ -2517,20 +2506,20 @@ def reflow(self, reflowed_lines, continued_indent,
break_after_open_bracket and index == 0 and
# Prefer to keep empty containers together instead of
# separating them.
unicode(item) == self.open_bracket and
(not next_item or unicode(next_item) != self.close_bracket) and
str(item) == self.open_bracket and
(not next_item or str(next_item) != self.close_bracket) and
(len(self._items) != 3 or not isinstance(next_item, Atom))
):
reflowed_lines.add_line_break(continued_indent)
break_after_open_bracket = False
else:
next_next_item = get_item(self._items, index + 2)
if (
unicode(item) not in ['.', '%', 'in'] and
str(item) not in ['.', '%', 'in'] and
next_item and not isinstance(next_item, Container) and
unicode(next_item) != ':' and
str(next_item) != ':' and
next_next_item and (not isinstance(next_next_item, Atom) or
unicode(next_item) == 'not') and
str(next_item) == 'not') and
not reflowed_lines.line_empty() and
not reflowed_lines.fits_on_current_line(
self._get_extent(index + 1) + 2)
Expand All @@ -2545,7 +2534,7 @@ def _get_extent(self, index):
"""
extent = 0
prev_item = get_item(self._items, index - 1)
seen_dot = prev_item and unicode(prev_item) == '.'
seen_dot = prev_item and str(prev_item) == '.'
while index < len(self._items):
item = get_item(self._items, index)
index += 1
Expand All @@ -2562,11 +2551,11 @@ def _get_extent(self, index):

prev_item = item
continue
elif (unicode(item) not in ['.', '=', ':', 'not'] and
elif (str(item) not in ['.', '=', ':', 'not'] and
not item.is_name and not item.is_string):
break

if unicode(item) == '.':
if str(item) == '.':
seen_dot = True

extent += item.size
Expand Down Expand Up @@ -2761,7 +2750,7 @@ def _reflow_lines(parsed_tokens, indentation, max_line_length,
start_on_prefix_line):
"""Reflow the lines so that it looks nice."""

if unicode(parsed_tokens[0]) == 'def':
if str(parsed_tokens[0]) == 'def':
# A function definition gets indented a bit more.
continued_indent = indentation + ' ' * 2 * DEFAULT_INDENT_SIZE
else:
Expand All @@ -2780,13 +2769,13 @@ def _reflow_lines(parsed_tokens, indentation, max_line_length,

if (
first_token and second_token and
unicode(second_token)[0] == '(' and
str(second_token)[0] == '(' and
len(indentation) + len(first_token) + 1 == len(continued_indent)
):
return None

for item in parsed_tokens:
lines.add_space_if_needed(unicode(item), equal=True)
lines.add_space_if_needed(str(item), equal=True)

save_continued_indent = continued_indent
if start_on_prefix_line and isinstance(item, Container):
Expand Down Expand Up @@ -3217,7 +3206,7 @@ def refactor_with_2to3(source_text, fixer_names, filename=''):
from lib2to3.pgen2 import tokenize as lib2to3_tokenize
try:
# The name parameter is necessary particularly for the "import" fixer.
return unicode(tool.refactor_string(source_text, name=filename))
return str(tool.refactor_string(source_text, name=filename))
except lib2to3_tokenize.TokenError:
return source_text

Expand Down Expand Up @@ -3501,13 +3490,13 @@ def mutual_startswith(a, b):

def code_match(code, select, ignore):
if ignore:
assert not isinstance(ignore, unicode)
assert not isinstance(ignore, str)
for ignored_code in [c.strip() for c in ignore]:
if mutual_startswith(code.lower(), ignored_code.lower()):
return False

if select:
assert not isinstance(select, unicode)
assert not isinstance(select, str)
for selected_code in [c.strip() for c in select]:
if mutual_startswith(code.lower(), selected_code.lower()):
return True
Expand All @@ -3524,7 +3513,7 @@ def fix_code(source, options=None, encoding=None, apply_config=False):
"""
options = _get_options(options, apply_config)

if not isinstance(source, unicode):
if not isinstance(source, str):
source = source.decode(encoding or get_encoding())

sio = io.StringIO(source)
Expand All @@ -3544,8 +3533,8 @@ def _get_options(raw_options, apply_config):

# Check for very basic type errors.
expected_type = type(getattr(options, name))
if not isinstance(expected_type, (str, unicode)):
if isinstance(value, (str, unicode)):
if not isinstance(expected_type, (str, )):
if isinstance(value, (str, )):
raise ValueError(
"Option '{}' should not be a string".format(name))
setattr(options, name, value)
Expand Down Expand Up @@ -4121,7 +4110,7 @@ def _split_comma_separated(string):

def decode_filename(filename):
"""Return Unicode filename."""
if isinstance(filename, unicode):
if isinstance(filename, str):
return filename

return filename.decode(sys.getfilesystemencoding())
Expand Down Expand Up @@ -4423,7 +4412,7 @@ def _fix_file(parameters):
try:
return fix_file(*parameters)
except IOError as error:
print(unicode(error), file=sys.stderr)
print(str(error), file=sys.stderr)
raise error


Expand Down
22 changes: 1 addition & 21 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import unittest
import warnings

try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from io import StringIO

ROOT_DIR = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]

Expand Down Expand Up @@ -7508,23 +7505,6 @@ def test_e501_experimental_with_in(self):
with autopep8_context(line, options=['--experimental']) as result:
self.assertEqual(fixed, result)

@unittest.skipIf(sys.version_info.major >= 3, 'syntax error in Python3')
def test_e501_print_isnot_function(self):
line = """\
def d():
print "%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d" % (111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
fixed = """\
def d():
print "%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d" % (
111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333,
333, 333, 333)
"""
with autopep8_context(line, options=['--experimental']) as result:
self.assertEqual(fixed, result)


@contextlib.contextmanager
def autopep8_context(line, options=None):
Expand Down

0 comments on commit e7b4944

Please sign in to comment.