Skip to content

Commit 0554c50

Browse files
Apply ruff/pyupgrade rule UP006
UP006 Use `dict` instead of `Dict` for type annotation UP006 Use `list` instead of `List` for type annotation UP006 Use `set` instead of `Set` for type annotation UP006 Use `tuple` instead of `Tuple` for type annotation
1 parent 1e18647 commit 0554c50

File tree

4 files changed

+50
-59
lines changed

4 files changed

+50
-59
lines changed

codespell_lib/_codespell.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@
2727
import textwrap
2828
from typing import (
2929
Any,
30-
Dict,
3130
Iterable,
32-
List,
3331
Match,
3432
Optional,
3533
Pattern,
3634
Sequence,
37-
Set,
3835
TextIO,
39-
Tuple,
4036
)
4137

4238
if sys.platform == "win32":
@@ -161,8 +157,8 @@ class QuietLevels:
161157

162158

163159
class GlobMatch:
164-
def __init__(self, pattern: List[str]) -> None:
165-
self.pattern_list: List[str] = pattern
160+
def __init__(self, pattern: list[str]) -> None:
161+
self.pattern_list: list[str] = pattern
166162

167163
def match(self, filename: str) -> bool:
168164
return any(fnmatch.fnmatch(filename, p) for p in self.pattern_list)
@@ -184,7 +180,7 @@ def disable(self) -> None:
184180

185181
class Summary:
186182
def __init__(self) -> None:
187-
self.summary: Dict[str, int] = {}
183+
self.summary: dict[str, int] = {}
188184

189185
def update(self, wrongword: str) -> None:
190186
if wrongword in self.summary:
@@ -227,12 +223,12 @@ def init_chardet(self) -> None:
227223

228224
self.encdetector = UniversalDetector()
229225

230-
def open(self, filename: str) -> Tuple[List[str], str]:
226+
def open(self, filename: str) -> tuple[list[str], str]:
231227
if self.use_chardet:
232228
return self.open_with_chardet(filename)
233229
return self.open_with_internal(filename)
234230

235-
def open_with_chardet(self, filename: str) -> Tuple[List[str], str]:
231+
def open_with_chardet(self, filename: str) -> tuple[list[str], str]:
236232
self.encdetector.reset()
237233
with open(filename, "rb") as fb:
238234
for line in fb:
@@ -259,7 +255,7 @@ def open_with_chardet(self, filename: str) -> Tuple[List[str], str]:
259255

260256
return lines, f.encoding
261257

262-
def open_with_internal(self, filename: str) -> Tuple[List[str], str]:
258+
def open_with_internal(self, filename: str) -> tuple[list[str], str]:
263259
encoding = None
264260
first_try = True
265261
for encoding in ("utf-8", "iso-8859-1"):
@@ -286,7 +282,7 @@ def open_with_internal(self, filename: str) -> Tuple[List[str], str]:
286282

287283
return lines, encoding
288284

289-
def get_lines(self, f: TextIO) -> List[str]:
285+
def get_lines(self, f: TextIO) -> list[str]:
290286
if self.ignore_multiline_regex:
291287
text = f.read()
292288
pos = 0
@@ -313,7 +309,7 @@ def get_lines(self, f: TextIO) -> List[str]:
313309
class NewlineHelpFormatter(argparse.HelpFormatter):
314310
"""Help formatter that preserves newlines and deals with lists."""
315311

316-
def _split_lines(self, text: str, width: int) -> List[str]:
312+
def _split_lines(self, text: str, width: int) -> list[str]:
317313
parts = text.split("\n")
318314
out = []
319315
for part in parts:
@@ -330,7 +326,7 @@ def _split_lines(self, text: str, width: int) -> List[str]:
330326
return out
331327

332328

333-
def _toml_to_parseconfig(toml_dict: Dict[str, Any]) -> Dict[str, Any]:
329+
def _toml_to_parseconfig(toml_dict: dict[str, Any]) -> dict[str, Any]:
334330
"""Convert a dict read from a TOML file to the parseconfig.read_dict() format."""
335331
return {
336332
k: "" if v is True else ",".join(v) if isinstance(v, list) else v
@@ -373,7 +369,7 @@ def _supports_ansi_colors() -> bool:
373369

374370
def parse_options(
375371
args: Sequence[str],
376-
) -> Tuple[argparse.Namespace, argparse.ArgumentParser, List[str]]:
372+
) -> tuple[argparse.Namespace, argparse.ArgumentParser, list[str]]:
377373
parser = argparse.ArgumentParser(formatter_class=NewlineHelpFormatter)
378374

379375
parser.set_defaults(colors=_supports_ansi_colors())
@@ -697,7 +693,7 @@ def parse_options(
697693

698694

699695
def process_ignore_words(
700-
words: Iterable[str], ignore_words: Set[str], ignore_words_cased: Set[str]
696+
words: Iterable[str], ignore_words: set[str], ignore_words_cased: set[str]
701697
) -> None:
702698
for word in words:
703699
word = word.strip()
@@ -708,10 +704,10 @@ def process_ignore_words(
708704

709705

710706
def parse_ignore_words_option(
711-
ignore_words_option: List[str],
712-
) -> Tuple[Set[str], Set[str]]:
713-
ignore_words: Set[str] = set()
714-
ignore_words_cased: Set[str] = set()
707+
ignore_words_option: list[str],
708+
) -> tuple[set[str], set[str]]:
709+
ignore_words: set[str] = set()
710+
ignore_words_cased: set[str] = set()
715711
if ignore_words_option:
716712
for comma_separated_words in ignore_words_option:
717713
process_ignore_words(
@@ -722,13 +718,13 @@ def parse_ignore_words_option(
722718
return (ignore_words, ignore_words_cased)
723719

724720

725-
def build_exclude_hashes(filename: str, exclude_lines: Set[str]) -> None:
721+
def build_exclude_hashes(filename: str, exclude_lines: set[str]) -> None:
726722
with open(filename, encoding="utf-8") as f:
727723
exclude_lines.update(line.rstrip() for line in f)
728724

729725

730726
def build_ignore_words(
731-
filename: str, ignore_words: Set[str], ignore_words_cased: Set[str]
727+
filename: str, ignore_words: set[str], ignore_words_cased: set[str]
732728
) -> None:
733729
with open(filename, encoding="utf-8") as f:
734730
process_ignore_words(
@@ -756,7 +752,7 @@ def ask_for_word_fix(
756752
misspelling: Misspelling,
757753
interactivity: int,
758754
colors: TermColors,
759-
) -> Tuple[bool, str]:
755+
) -> tuple[bool, str]:
760756
wrongword = match.group()
761757
if interactivity <= 0:
762758
return misspelling.fix, fix_case(wrongword, misspelling.data)
@@ -813,9 +809,9 @@ def ask_for_word_fix(
813809

814810

815811
def print_context(
816-
lines: List[str],
812+
lines: list[str],
817813
index: int,
818-
context: Tuple[int, int],
814+
context: tuple[int, int],
819815
) -> None:
820816
# context = (context_before, context_after)
821817
for i in range(index - context[0], index + context[1] + 1):
@@ -836,26 +832,26 @@ def extract_words(
836832
text: str,
837833
word_regex: Pattern[str],
838834
ignore_word_regex: Optional[Pattern[str]],
839-
) -> List[str]:
835+
) -> list[str]:
840836
return word_regex.findall(_ignore_word_sub(text, ignore_word_regex))
841837

842838

843839
def extract_words_iter(
844840
text: str,
845841
word_regex: Pattern[str],
846842
ignore_word_regex: Optional[Pattern[str]],
847-
) -> List[Match[str]]:
843+
) -> list[Match[str]]:
848844
return list(word_regex.finditer(_ignore_word_sub(text, ignore_word_regex)))
849845

850846

851847
def apply_uri_ignore_words(
852-
check_matches: List[Match[str]],
848+
check_matches: list[Match[str]],
853849
line: str,
854850
word_regex: Pattern[str],
855851
ignore_word_regex: Optional[Pattern[str]],
856852
uri_regex: Pattern[str],
857-
uri_ignore_words: Set[str],
858-
) -> List[Match[str]]:
853+
uri_ignore_words: set[str],
854+
) -> list[Match[str]]:
859855
if not uri_ignore_words:
860856
return check_matches
861857
for uri in uri_regex.findall(line):
@@ -873,15 +869,15 @@ def parse_file(
873869
filename: str,
874870
colors: TermColors,
875871
summary: Optional[Summary],
876-
misspellings: Dict[str, Misspelling],
877-
ignore_words_cased: Set[str],
878-
exclude_lines: Set[str],
872+
misspellings: dict[str, Misspelling],
873+
ignore_words_cased: set[str],
874+
exclude_lines: set[str],
879875
file_opener: FileOpener,
880876
word_regex: Pattern[str],
881877
ignore_word_regex: Optional[Pattern[str]],
882878
uri_regex: Pattern[str],
883-
uri_ignore_words: Set[str],
884-
context: Optional[Tuple[int, int]],
879+
uri_ignore_words: set[str],
880+
context: Optional[tuple[int, int]],
885881
options: argparse.Namespace,
886882
) -> int:
887883
bad_count = 0
@@ -1085,7 +1081,7 @@ def parse_file(
10851081

10861082
def flatten_clean_comma_separated_arguments(
10871083
arguments: Iterable[str],
1088-
) -> List[str]:
1084+
) -> list[str]:
10891085
"""
10901086
>>> flatten_clean_comma_separated_arguments(["a, b ,\n c, d,", "e"])
10911087
['a', 'b', 'c', 'd', 'e']
@@ -1227,7 +1223,7 @@ def main(*args: str) -> int:
12271223
f"ERROR: cannot find dictionary file: {dictionary}",
12281224
)
12291225
use_dictionaries.append(dictionary)
1230-
misspellings: Dict[str, Misspelling] = {}
1226+
misspellings: dict[str, Misspelling] = {}
12311227
for dictionary in use_dictionaries:
12321228
build_dict(dictionary, misspellings, ignore_words)
12331229
colors = TermColors()
@@ -1255,7 +1251,7 @@ def main(*args: str) -> int:
12551251
context_after = max(0, options.after_context)
12561252
context = (context_before, context_after)
12571253

1258-
exclude_lines: Set[str] = set()
1254+
exclude_lines: set[str] = set()
12591255
if options.exclude_file:
12601256
exclude_files = flatten_clean_comma_separated_arguments(options.exclude_file)
12611257
for exclude_file in exclude_files:

codespell_lib/_spellchecker.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
Copyright (C) 2011 ProFUSION embedded systems
1717
"""
1818

19-
from typing import (
20-
Dict,
21-
Set,
22-
)
23-
2419
# Pass all misspellings through this translation table to generate
2520
# alternative misspellings and fixes.
2621
alt_chars = (("'", "’"),) # noqa: RUF001
@@ -36,7 +31,7 @@ def __init__(self, data: str, fix: bool, reason: str) -> None:
3631
def add_misspelling(
3732
key: str,
3833
data: str,
39-
misspellings: Dict[str, Misspelling],
34+
misspellings: dict[str, Misspelling],
4035
) -> None:
4136
data = data.strip()
4237

@@ -53,8 +48,8 @@ def add_misspelling(
5348

5449
def build_dict(
5550
filename: str,
56-
misspellings: Dict[str, Misspelling],
57-
ignore_words: Set[str],
51+
misspellings: dict[str, Misspelling],
52+
ignore_words: set[str],
5853
) -> None:
5954
with open(filename, encoding="utf-8") as f:
6055
translate_tables = [(x, str.maketrans(x, y)) for x, y in alt_chars]

codespell_lib/tests/test_basic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from io import StringIO
99
from pathlib import Path
1010
from shutil import copyfile
11-
from typing import Any, Generator, Optional, Tuple, Union
11+
from typing import Any, Generator, Optional, Union
1212
from unittest import mock
1313

1414
import pytest
@@ -39,7 +39,7 @@ def main(
3939
*args: Any,
4040
count: bool = True,
4141
std: bool = False,
42-
) -> Union[int, Tuple[int, str, str]]:
42+
) -> Union[int, tuple[int, str, str]]:
4343
args = tuple(str(arg) for arg in args)
4444
if count:
4545
args = ("--count", *args)
@@ -65,7 +65,7 @@ def main(
6565

6666

6767
def run_codespell(
68-
args: Tuple[Any, ...] = (),
68+
args: tuple[Any, ...] = (),
6969
cwd: Optional[Path] = None,
7070
) -> int:
7171
"""Run codespell."""
@@ -1373,7 +1373,7 @@ def FakeStdin(text: str) -> Generator[None, None, None]:
13731373

13741374
def run_codespell_stdin(
13751375
text: str,
1376-
args: Tuple[Any, ...],
1376+
args: tuple[Any, ...],
13771377
cwd: Optional[Path] = None,
13781378
) -> int:
13791379
"""Run codespell in stdin mode and return number of lines in output."""
@@ -1397,7 +1397,7 @@ def test_stdin(tmp_path: Path) -> None:
13971397
for _ in range(input_file_lines):
13981398
text += "abandonned\n"
13991399
for single_line_per_error in (True, False):
1400-
args: Tuple[str, ...] = ()
1400+
args: tuple[str, ...] = ()
14011401
if single_line_per_error:
14021402
args = ("--stdin-single-line",)
14031403
# we expect 'input_file_lines' number of lines with

codespell_lib/tests/test_dictionary.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os.path as op
44
import pathlib
55
import re
6-
from typing import Any, Dict, Iterable, Optional, Set, Tuple
6+
from typing import Any, Iterable, Optional
77

88
import pytest
99

@@ -37,8 +37,8 @@
3737
)
3838
raise RuntimeError(msg) from e
3939

40-
global_err_dicts: Dict[str, Dict[str, Any]] = {}
41-
global_pairs: Set[Tuple[str, str]] = set()
40+
global_err_dicts: dict[str, dict[str, Any]] = {}
41+
global_pairs: set[tuple[str, str]] = set()
4242

4343
# Filename, should be seen as errors in aspell or not
4444
_data_dir = op.join(op.dirname(__file__), "..", "data")
@@ -61,8 +61,8 @@ def test_dictionaries_exist() -> None:
6161
@fname_params
6262
def test_dictionary_formatting(
6363
fname: str,
64-
in_aspell: Tuple[bool, bool],
65-
in_dictionary: Tuple[Iterable[str], Iterable[str]],
64+
in_aspell: tuple[bool, bool],
65+
in_dictionary: tuple[Iterable[str], Iterable[str]],
6666
) -> None:
6767
"""Test that all dictionary entries are valid."""
6868
errors = []
@@ -137,9 +137,9 @@ def _check_aspell(
137137
def _check_err_rep(
138138
err: str,
139139
rep: str,
140-
in_aspell: Tuple[Optional[bool], Optional[bool]],
140+
in_aspell: tuple[Optional[bool], Optional[bool]],
141141
fname: str,
142-
languages: Tuple[Iterable[str], Iterable[str]],
142+
languages: tuple[Iterable[str], Iterable[str]],
143143
) -> None:
144144
assert whitespace.search(err) is None, f"error {err!r} has whitespace"
145145
assert "," not in err, f"error {err!r} has a comma"
@@ -296,8 +296,8 @@ def test_error_checking_in_aspell(
296296
@pytest.mark.dependency(name="dictionary loop")
297297
def test_dictionary_looping(
298298
fname: str,
299-
in_aspell: Tuple[bool, bool],
300-
in_dictionary: Tuple[bool, bool],
299+
in_aspell: tuple[bool, bool],
300+
in_dictionary: tuple[bool, bool],
301301
) -> None:
302302
"""Test that all dictionary entries are valid."""
303303
this_err_dict = {}

0 commit comments

Comments
 (0)