Skip to content

Commit

Permalink
Apply pyupgrade to project (#2364)
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 Oct 18, 2022
1 parent a8a5228 commit d31e0df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
19 changes: 9 additions & 10 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -83,7 +82,7 @@
# file1 .. fileN Files to check spelling


class QuietLevels(object):
class QuietLevels:
NONE = 0
ENCODING = 1
BINARY_FILE = 2
Expand All @@ -92,7 +91,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 @@ -111,14 +110,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 @@ -132,7 +131,7 @@ def disable(self):
self.DISABLE = ''


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

Expand All @@ -152,7 +151,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
# Encoding detection (only try ISO 8859-1 because UTF-8 is the default)
with open(f.name, 'wb') as f:
Expand Down Expand Up @@ -395,7 +393,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

0 comments on commit d31e0df

Please sign in to comment.