Apply pyupgrade to project (#2364)
Update patterns to modern Python syntax and features. Drops unnecessary
legacy syntax that was required for Python 2 (removed in
9c3db1a5c5e02739eedba9e9915acd51736300b3).
Details on pyupgrade can be found at:
https://github.com/asottile/pyupgrade
diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py
index ff15524..598aa72 100644
--- a/codespell_lib/_codespell.py
+++ b/codespell_lib/_codespell.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -29,12 +28,12 @@
# autogenerated by setuptools_scm
from ._version import __version__ as VERSION
-word_regex_def = u"[\\w\\-'’`]+"
+word_regex_def = "[\\w\\-'’`]+"
# While we want to treat characters like ( or " as okay for a starting break,
# these may occur unescaped in URIs, and so we are more restrictive on the
# endpoint. Emails are more restrictive, so the endpoint remains flexible.
-uri_regex_def = (u"(\\b(?:https?|[ts]?ftp|file|git|smb)://[^\\s]+(?=$|\\s)|"
- u"\\b[\\w.%+-]+@[\\w.-]+\\b)")
+uri_regex_def = ("(\\b(?:https?|[ts]?ftp|file|git|smb)://[^\\s]+(?=$|\\s)|"
+ "\\b[\\w.%+-]+@[\\w.-]+\\b)")
encodings = ('utf-8', 'iso-8859-1')
USAGE = """
\t%prog [OPTIONS] [file1 file2 ... fileN]
@@ -83,7 +82,7 @@
# file1 .. fileN Files to check spelling
-class QuietLevels(object):
+class QuietLevels:
NONE = 0
ENCODING = 1
BINARY_FILE = 2
@@ -92,7 +91,7 @@
FIXES = 16
-class GlobMatch(object):
+class GlobMatch:
def __init__(self, pattern):
if pattern:
# Pattern might be a list of comma-delimited strings
@@ -111,14 +110,14 @@
return False
-class Misspelling(object):
+class Misspelling:
def __init__(self, data, fix, reason):
self.data = data
self.fix = fix
self.reason = reason
-class TermColors(object):
+class TermColors:
def __init__(self):
self.FILE = '\033[33m'
self.WWORD = '\033[31m'
@@ -132,7 +131,7 @@
self.DISABLE = ''
-class Summary(object):
+class Summary:
def __init__(self):
self.summary = {}
@@ -152,7 +151,7 @@
width=15 - len(key)) for key in keys])
-class FileOpener(object):
+class FileOpener:
def __init__(self, use_chardet, quiet_level):
self.use_chardet = use_chardet
if use_chardet:
diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py
index 49f81ce..8b73584 100644
--- a/codespell_lib/tests/test_basic.py
+++ b/codespell_lib/tests/test_basic.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
import contextlib
import inspect
import os
@@ -22,7 +20,7 @@
assert EX_DATAERR == 65
-class MainWrapper(object):
+class MainWrapper:
"""Compatibility wrapper for when we used to return the count."""
def main(self, *args, count=True, std=False, **kwargs):
@@ -174,7 +172,7 @@
with FakeStdin('0\n'): # blank input -> nothing
assert cs.main('-w', '-i', '3', f.name) == 0
assert cs.main(f.name) == 0
- with open(f.name, 'r') as f_read:
+ with open(f.name) as f_read:
assert f_read.read() == 'awkward\n'
with open(f.name, 'w') as f:
f.write('ackward\n')
@@ -184,7 +182,7 @@
assert code == 0
assert 'a valid option' in stdout
assert cs.main(f.name) == 0
- with open(f.name, 'r') as f:
+ with open(f.name) as f:
assert f.read() == 'backward\n'
finally:
os.remove(f.name)
@@ -249,11 +247,11 @@
"""Test exclude file functionality."""
d = str(tmpdir)
with open(op.join(d, 'bad.txt'), 'wb') as f:
- f.write('1 abandonned 1\n2 abandonned 2\n'.encode('utf-8'))
+ f.write(b'1 abandonned 1\n2 abandonned 2\n')
bad_name = f.name
assert cs.main(bad_name) == 2
with open(op.join(d, 'tmp.txt'), 'wb') as f:
- f.write('1 abandonned 1\n'.encode('utf-8'))
+ f.write(b'1 abandonned 1\n')
assert cs.main(bad_name) == 2
assert cs.main('-x', f.name, bad_name) == 1
@@ -266,11 +264,11 @@
# with CaptureStdout() as sio:
assert cs.main(f.name) == 0
with open(f.name, 'wb') as f:
- f.write(u'naïve\n'.encode('utf-8'))
+ f.write('naïve\n'.encode())
assert cs.main(f.name) == 0
assert cs.main('-e', f.name) == 0
with open(f.name, 'ab') as f:
- f.write(u'naieve\n'.encode('utf-8'))
+ f.write(b'naieve\n')
assert cs.main(f.name) == 1
# Encoding detection (only try ISO 8859-1 because UTF-8 is the default)
with open(f.name, 'wb') as f:
@@ -395,7 +393,7 @@
# with CaptureStdout() as sio:
assert cs.main(f.name) == 0
with open(f.name, 'wb') as f:
- f.write('this has an ACII error'.encode('utf-8'))
+ f.write(b'this has an ACII error')
code, stdout, _ = cs.main(f.name, std=True)
assert code == 1
assert 'ASCII' in stdout
diff --git a/codespell_lib/tests/test_dictionary.py b/codespell_lib/tests/test_dictionary.py
index 6d246d3..9f3dec0 100644
--- a/codespell_lib/tests/test_dictionary.py
+++ b/codespell_lib/tests/test_dictionary.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
import glob
import os.path as op
import os
@@ -41,9 +39,9 @@
def test_dictionaries_exist():
"""Test consistency of dictionaries."""
- doc_fnames = set(op.basename(f[0]) for f in _fnames_in_aspell)
- got_fnames = set(op.basename(f)
- for f in glob.glob(op.join(_data_dir, '*.txt')))
+ doc_fnames = {op.basename(f[0]) for f in _fnames_in_aspell}
+ got_fnames = {op.basename(f)
+ for f in glob.glob(op.join(_data_dir, '*.txt'))}
assert doc_fnames == got_fnames