Skip to content

Commit 0d292d5

Browse files
committed
Drop old code
Python 3.6 (and 2) are no longer supported, so we can require Python 3.7 and drop all the legacy stuff. Supersedes #111.
1 parent a099b10 commit 0d292d5

File tree

6 files changed

+29
-51
lines changed

6 files changed

+29
-51
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ repos:
1717
rev: 5.0.4
1818
hooks:
1919
- id: flake8
20+
21+
- repo: https://github.com/asottile/pyupgrade
22+
rev: v2.37.3
23+
hooks:
24+
- id: pyupgrade
25+
args:
26+
- --py37-plus

autoflake.py

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,10 @@
4848
ATOMS = frozenset([tokenize.NAME, tokenize.NUMBER, tokenize.STRING])
4949

5050
EXCEPT_REGEX = re.compile(r'^\s*except [\s,()\w]+ as \w+:$')
51-
PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[23]?\b\s*$')
51+
PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[3]?\b\s*$')
5252

5353
MAX_PYTHON_FILE_DETECTION_BYTES = 1024
5454

55-
try:
56-
unicode
57-
except NameError:
58-
unicode = str
59-
60-
61-
try:
62-
RecursionError
63-
except NameError:
64-
# Python before 3.5.
65-
RecursionError = RuntimeError
66-
6755

6856
def standard_paths():
6957
"""Yield paths to standard modules."""
@@ -72,14 +60,12 @@ def standard_paths():
7260
# Yield lib paths.
7361
path = distutils.sysconfig.get_python_lib(standard_lib=True,
7462
plat_specific=is_plat_spec)
75-
for name in os.listdir(path):
76-
yield name
63+
yield from os.listdir(path)
7764

7865
# Yield lib-dynload paths.
7966
dynload_path = os.path.join(path, 'lib-dynload')
8067
if os.path.isdir(dynload_path):
81-
for name in os.listdir(dynload_path):
82-
yield name
68+
yield from os.listdir(dynload_path)
8369

8470

8571
def standard_package_names():
@@ -118,8 +104,8 @@ def unused_import_module_name(messages):
118104
for message in messages:
119105
if isinstance(message, pyflakes.messages.UnusedImport):
120106
module_name = re.search(pattern, str(message))
121-
module_name = module_name.group()[1:-1]
122107
if module_name:
108+
module_name = module_name.group()[1:-1]
123109
yield (message.lineno, module_name)
124110

125111

@@ -184,15 +170,6 @@ def create_key_to_messages_dict(messages):
184170

185171
def check(source):
186172
"""Return messages from pyflakes."""
187-
if sys.version_info[0] == 2 and isinstance(source, unicode):
188-
# Convert back to original byte string encoding, otherwise pyflakes
189-
# call to compile() will complain. See PEP 263. This only affects
190-
# Python 2.
191-
try:
192-
source = source.encode('utf-8')
193-
except UnicodeError: # pragma: no cover
194-
return []
195-
196173
reporter = ListReporter()
197174
try:
198175
pyflakes.api.check(source, filename='<string>', reporter=reporter)
@@ -201,7 +178,7 @@ def check(source):
201178
return reporter.messages
202179

203180

204-
class StubFile(object):
181+
class StubFile:
205182
"""Stub out file for pyflakes."""
206183

207184
def write(self, *_):
@@ -267,7 +244,7 @@ def multiline_statement(line, previous_line=''):
267244
return True
268245

269246

270-
class PendingFix(object):
247+
class PendingFix:
271248
"""Allows a rewrite operation to span multiple lines.
272249
273250
In the main rewrite loop, every time a helper function returns a
@@ -835,8 +812,8 @@ def _fix_file(input_file, filename, args, write_to_stdout, standard_out,
835812
if original_source != filtered_source:
836813
if args.check:
837814
standard_out.write(
838-
'{filename}: Unused imports/variables detected\n'.format(
839-
filename=filename))
815+
f'{filename}: Unused imports/variables detected\n',
816+
)
840817
sys.exit(1)
841818
if write_to_stdout:
842819
standard_out.write(filtered_source)
@@ -866,8 +843,8 @@ def open_with_encoding(filename, encoding, mode='r',
866843
if not encoding:
867844
encoding = detect_encoding(filename, limit_byte_check=limit_byte_check)
868845

869-
return io.open(filename, mode=mode, encoding=encoding,
870-
newline='') # Preserve line endings
846+
return open(filename, mode=mode, encoding=encoding,
847+
newline='') # Preserve line endings
871848

872849

873850
def detect_encoding(filename, limit_byte_check=-1):
@@ -934,7 +911,7 @@ def is_python_file(filename):
934911
if not text:
935912
return False
936913
first_line = text.splitlines()[0]
937-
except (IOError, IndexError):
914+
except (OSError, IndexError):
938915
return False
939916

940917
if not PYTHON_SHEBANG_REGEX.match(first_line):
@@ -1075,8 +1052,8 @@ def _main(argv, standard_out, standard_error, standard_input=None):
10751052
else:
10761053
try:
10771054
fix_file(name, args=args, standard_out=standard_out)
1078-
except IOError as exception:
1079-
_LOGGER.error(unicode(exception))
1055+
except OSError as exception:
1056+
_LOGGER.error(str(exception))
10801057
failure = True
10811058

10821059
return 1 if failure else 0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def version():
2323
license='Expat License',
2424
author='Steven Myint',
2525
url='https://github.com/myint/autoflake',
26+
python_requires='>=3.7',
2627
classifiers=['Environment :: Console',
2728
'Intended Audience :: Developers',
2829
'License :: OSI Approved :: MIT License',

test_autoflake.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# coding: utf-8
32
"""Test suite for autoflake."""
43
import contextlib
54
import functools
@@ -2007,7 +2006,7 @@ def test_fix_without_from(self):
20072006
'\n'
20082007
)
20092008

2010-
self.unused = ['lib{}.x.y.z'.format(x) for x in (1, 3, 4)]
2009+
self.unused = [f'lib{x}.x.y.z' for x in (1, 3, 4)]
20112010
self.assert_fix([
20122011
'import \\\n',
20132012
' lib1.x.y.z \\',
@@ -2201,7 +2200,7 @@ def temporary_directory(directory='.', prefix='tmp.'):
22012200
shutil.rmtree(temp_directory)
22022201

22032202

2204-
class StubFile(object):
2203+
class StubFile:
22052204

22062205
"""Fake file that ignores everything."""
22072206

test_fuzz.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
END = ''
2626

2727

28-
try:
29-
unicode
30-
except NameError:
31-
unicode = str
32-
33-
3428
def colored(text, color):
3529
"""Return color coded text."""
3630
return color + text + END
@@ -106,7 +100,7 @@ def run(filename, command, verbose=False, options=None):
106100
if file_diff and after_count > before_count:
107101
sys.stderr.write('autoflake made ' + filename + ' worse\n')
108102
return False
109-
except IOError as exception:
103+
except OSError as exception:
110104
sys.stderr.write(str(exception) + '\n')
111105

112106
return True
@@ -209,7 +203,7 @@ def check(args):
209203
completed_filenames.update(name)
210204

211205
if os.path.isdir(name):
212-
for root, directories, children in os.walk(unicode(name)):
206+
for root, directories, children in os.walk(name):
213207
filenames += [os.path.join(root, f) for f in children
214208
if f.endswith('.py') and
215209
not f.startswith('.')]

test_fuzz_pypi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def latest_packages(last_hours):
1717
"""Return names of latest released packages on PyPI."""
1818
process = subprocess.Popen(
19-
['yolk', '--latest-releases={hours}'.format(hours=last_hours)],
19+
['yolk', f'--latest-releases={last_hours}'],
2020
stdout=subprocess.PIPE)
2121

2222
for line in process.communicate()[0].decode('utf-8').split('\n'):
@@ -29,7 +29,7 @@ def download_package(name, output_directory):
2929
3030
Raise CalledProcessError on failure.
3131
"""
32-
subprocess.check_call(['yolk', '--fetch-package={name}'.format(name=name)],
32+
subprocess.check_call(['yolk', f'--fetch-package={name}'],
3333
cwd=output_directory)
3434

3535

@@ -41,14 +41,14 @@ def extract_package(path, output_directory):
4141
tar.extractall(path=output_directory)
4242
tar.close()
4343
return True
44-
except (tarfile.ReadError, IOError):
44+
except (tarfile.ReadError, OSError):
4545
return False
4646
elif path.lower().endswith('.zip'):
4747
try:
4848
archive = zipfile.ZipFile(path)
4949
archive.extractall(path=output_directory)
5050
archive.close()
51-
except (zipfile.BadZipfile, IOError):
51+
except (zipfile.BadZipfile, OSError):
5252
return False
5353
return True
5454

0 commit comments

Comments
 (0)