Skip to content

Commit

Permalink
Apply pyupgrade to project
Browse files Browse the repository at this point in the history
Update patterns to modern Python syntax and features. Drops unnecessary
legacy syntax that was required for Python 2 (removed in
9c3db1a).

Details on pyupgrade can be found at:
https://github.com/asottile/pyupgrade
  • Loading branch information
jdufresne committed Jun 12, 2022
1 parent 1b41c9e commit 7e47275
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
20 changes: 9 additions & 11 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
@@ -1,5 +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
# the Free Software Foundation; version 2 of the License.
Expand All @@ -26,12 +24,12 @@
import sys
import textwrap

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]
Expand Down Expand Up @@ -81,7 +79,7 @@
# file1 .. fileN Files to check spelling


class QuietLevels(object):
class QuietLevels:
NONE = 0
ENCODING = 1
BINARY_FILE = 2
Expand All @@ -90,7 +88,7 @@ class QuietLevels(object):
FIXES = 16


class GlobMatch(object):
class GlobMatch:
def __init__(self, pattern):
if pattern:
# Pattern might be a list of comma-delimited strings
Expand All @@ -109,14 +107,14 @@ def match(self, filename):
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'
Expand All @@ -130,7 +128,7 @@ def disable(self):
self.DISABLE = ''


class Summary(object):
class Summary:
def __init__(self):
self.summary = {}

Expand All @@ -150,7 +148,7 @@ def __str__(self):
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:
Expand Down
18 changes: 8 additions & 10 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import contextlib
import inspect
import os
Expand All @@ -22,7 +20,7 @@ def test_constants():
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):
Expand Down Expand Up @@ -174,7 +172,7 @@ def test_interactivity(tmpdir, capsys):
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')
Expand All @@ -184,7 +182,7 @@ def test_interactivity(tmpdir, capsys):
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)
Expand Down Expand Up @@ -249,11 +247,11 @@ def test_exclude_file(tmpdir, capsys):
"""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

Expand All @@ -266,11 +264,11 @@ def test_encoding(tmpdir, capsys):
# 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
# Binary file warning
with open(f.name, 'wb') as f:
Expand Down Expand Up @@ -382,7 +380,7 @@ def test_case_handling(tmpdir, capsys):
# 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
Expand Down
8 changes: 3 additions & 5 deletions codespell_lib/tests/test_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import glob
import os.path as op
import os
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
URL = 'https://github.com/codespell-project/codespell/'
LICENSE = 'GPL v2'
DOWNLOAD_URL = 'https://github.com/codespell-project/codespell/'
with open('README.rst', 'r') as f:
with open('README.rst') as f:
LONG_DESCRIPTION = f.read()

if __name__ == "__main__":
Expand Down

0 comments on commit 7e47275

Please sign in to comment.