25
25
import re
26
26
import sys
27
27
import textwrap
28
+ from collections .abc import Iterable , Sequence
29
+ from re import Match , Pattern
28
30
from typing import (
29
31
Any ,
30
- Dict ,
31
- Iterable ,
32
- List ,
33
- Match ,
34
32
Optional ,
35
- Pattern ,
36
- Sequence ,
37
- Set ,
38
33
TextIO ,
39
- Tuple ,
40
34
)
41
35
42
36
if sys .platform == "win32" :
@@ -161,8 +155,8 @@ class QuietLevels:
161
155
162
156
163
157
class GlobMatch :
164
- def __init__ (self , pattern : List [str ]) -> None :
165
- self .pattern_list : List [str ] = pattern
158
+ def __init__ (self , pattern : list [str ]) -> None :
159
+ self .pattern_list : list [str ] = pattern
166
160
167
161
def match (self , filename : str ) -> bool :
168
162
return any (fnmatch .fnmatch (filename , p ) for p in self .pattern_list )
@@ -184,7 +178,7 @@ def disable(self) -> None:
184
178
185
179
class Summary :
186
180
def __init__ (self ) -> None :
187
- self .summary : Dict [str , int ] = {}
181
+ self .summary : dict [str , int ] = {}
188
182
189
183
def update (self , wrongword : str ) -> None :
190
184
if wrongword in self .summary :
@@ -227,12 +221,12 @@ def init_chardet(self) -> None:
227
221
228
222
self .encdetector = UniversalDetector ()
229
223
230
- def open (self , filename : str ) -> Tuple [ List [str ], str ]:
224
+ def open (self , filename : str ) -> tuple [ list [str ], str ]:
231
225
if self .use_chardet :
232
226
return self .open_with_chardet (filename )
233
227
return self .open_with_internal (filename )
234
228
235
- def open_with_chardet (self , filename : str ) -> Tuple [ List [str ], str ]:
229
+ def open_with_chardet (self , filename : str ) -> tuple [ list [str ], str ]:
236
230
self .encdetector .reset ()
237
231
with open (filename , "rb" ) as fb :
238
232
for line in fb :
@@ -259,7 +253,7 @@ def open_with_chardet(self, filename: str) -> Tuple[List[str], str]:
259
253
260
254
return lines , f .encoding
261
255
262
- def open_with_internal (self , filename : str ) -> Tuple [ List [str ], str ]:
256
+ def open_with_internal (self , filename : str ) -> tuple [ list [str ], str ]:
263
257
encoding = None
264
258
first_try = True
265
259
for encoding in ("utf-8" , "iso-8859-1" ):
@@ -286,7 +280,7 @@ def open_with_internal(self, filename: str) -> Tuple[List[str], str]:
286
280
287
281
return lines , encoding
288
282
289
- def get_lines (self , f : TextIO ) -> List [str ]:
283
+ def get_lines (self , f : TextIO ) -> list [str ]:
290
284
if self .ignore_multiline_regex :
291
285
text = f .read ()
292
286
pos = 0
@@ -313,7 +307,7 @@ def get_lines(self, f: TextIO) -> List[str]:
313
307
class NewlineHelpFormatter (argparse .HelpFormatter ):
314
308
"""Help formatter that preserves newlines and deals with lists."""
315
309
316
- def _split_lines (self , text : str , width : int ) -> List [str ]:
310
+ def _split_lines (self , text : str , width : int ) -> list [str ]:
317
311
parts = text .split ("\n " )
318
312
out = []
319
313
for part in parts :
@@ -330,7 +324,7 @@ def _split_lines(self, text: str, width: int) -> List[str]:
330
324
return out
331
325
332
326
333
- def _toml_to_parseconfig (toml_dict : Dict [str , Any ]) -> Dict [str , Any ]:
327
+ def _toml_to_parseconfig (toml_dict : dict [str , Any ]) -> dict [str , Any ]:
334
328
"""Convert a dict read from a TOML file to the parseconfig.read_dict() format."""
335
329
return {
336
330
k : "" if v is True else "," .join (v ) if isinstance (v , list ) else v
@@ -373,7 +367,7 @@ def _supports_ansi_colors() -> bool:
373
367
374
368
def parse_options (
375
369
args : Sequence [str ],
376
- ) -> Tuple [argparse .Namespace , argparse .ArgumentParser , List [str ]]:
370
+ ) -> tuple [argparse .Namespace , argparse .ArgumentParser , list [str ]]:
377
371
parser = argparse .ArgumentParser (formatter_class = NewlineHelpFormatter )
378
372
379
373
parser .set_defaults (colors = _supports_ansi_colors ())
@@ -697,7 +691,7 @@ def parse_options(
697
691
698
692
699
693
def process_ignore_words (
700
- words : Iterable [str ], ignore_words : Set [str ], ignore_words_cased : Set [str ]
694
+ words : Iterable [str ], ignore_words : set [str ], ignore_words_cased : set [str ]
701
695
) -> None :
702
696
for word in words :
703
697
word = word .strip ()
@@ -708,10 +702,10 @@ def process_ignore_words(
708
702
709
703
710
704
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 ()
705
+ ignore_words_option : list [str ],
706
+ ) -> tuple [ set [str ], set [str ]]:
707
+ ignore_words : set [str ] = set ()
708
+ ignore_words_cased : set [str ] = set ()
715
709
if ignore_words_option :
716
710
for comma_separated_words in ignore_words_option :
717
711
process_ignore_words (
@@ -722,13 +716,13 @@ def parse_ignore_words_option(
722
716
return (ignore_words , ignore_words_cased )
723
717
724
718
725
- def build_exclude_hashes (filename : str , exclude_lines : Set [str ]) -> None :
719
+ def build_exclude_hashes (filename : str , exclude_lines : set [str ]) -> None :
726
720
with open (filename , encoding = "utf-8" ) as f :
727
721
exclude_lines .update (line .rstrip () for line in f )
728
722
729
723
730
724
def build_ignore_words (
731
- filename : str , ignore_words : Set [str ], ignore_words_cased : Set [str ]
725
+ filename : str , ignore_words : set [str ], ignore_words_cased : set [str ]
732
726
) -> None :
733
727
with open (filename , encoding = "utf-8" ) as f :
734
728
process_ignore_words (
@@ -756,7 +750,7 @@ def ask_for_word_fix(
756
750
misspelling : Misspelling ,
757
751
interactivity : int ,
758
752
colors : TermColors ,
759
- ) -> Tuple [bool , str ]:
753
+ ) -> tuple [bool , str ]:
760
754
wrongword = match .group ()
761
755
if interactivity <= 0 :
762
756
return misspelling .fix , fix_case (wrongword , misspelling .data )
@@ -813,9 +807,9 @@ def ask_for_word_fix(
813
807
814
808
815
809
def print_context (
816
- lines : List [str ],
810
+ lines : list [str ],
817
811
index : int ,
818
- context : Tuple [int , int ],
812
+ context : tuple [int , int ],
819
813
) -> None :
820
814
# context = (context_before, context_after)
821
815
for i in range (index - context [0 ], index + context [1 ] + 1 ):
@@ -836,26 +830,26 @@ def extract_words(
836
830
text : str ,
837
831
word_regex : Pattern [str ],
838
832
ignore_word_regex : Optional [Pattern [str ]],
839
- ) -> List [str ]:
833
+ ) -> list [str ]:
840
834
return word_regex .findall (_ignore_word_sub (text , ignore_word_regex ))
841
835
842
836
843
837
def extract_words_iter (
844
838
text : str ,
845
839
word_regex : Pattern [str ],
846
840
ignore_word_regex : Optional [Pattern [str ]],
847
- ) -> List [Match [str ]]:
841
+ ) -> list [Match [str ]]:
848
842
return list (word_regex .finditer (_ignore_word_sub (text , ignore_word_regex )))
849
843
850
844
851
845
def apply_uri_ignore_words (
852
- check_matches : List [Match [str ]],
846
+ check_matches : list [Match [str ]],
853
847
line : str ,
854
848
word_regex : Pattern [str ],
855
849
ignore_word_regex : Optional [Pattern [str ]],
856
850
uri_regex : Pattern [str ],
857
- uri_ignore_words : Set [str ],
858
- ) -> List [Match [str ]]:
851
+ uri_ignore_words : set [str ],
852
+ ) -> list [Match [str ]]:
859
853
if not uri_ignore_words :
860
854
return check_matches
861
855
for uri in uri_regex .findall (line ):
@@ -873,15 +867,15 @@ def parse_file(
873
867
filename : str ,
874
868
colors : TermColors ,
875
869
summary : Optional [Summary ],
876
- misspellings : Dict [str , Misspelling ],
877
- ignore_words_cased : Set [str ],
878
- exclude_lines : Set [str ],
870
+ misspellings : dict [str , Misspelling ],
871
+ ignore_words_cased : set [str ],
872
+ exclude_lines : set [str ],
879
873
file_opener : FileOpener ,
880
874
word_regex : Pattern [str ],
881
875
ignore_word_regex : Optional [Pattern [str ]],
882
876
uri_regex : Pattern [str ],
883
- uri_ignore_words : Set [str ],
884
- context : Optional [Tuple [int , int ]],
877
+ uri_ignore_words : set [str ],
878
+ context : Optional [tuple [int , int ]],
885
879
options : argparse .Namespace ,
886
880
) -> int :
887
881
bad_count = 0
@@ -1085,7 +1079,7 @@ def parse_file(
1085
1079
1086
1080
def flatten_clean_comma_separated_arguments (
1087
1081
arguments : Iterable [str ],
1088
- ) -> List [str ]:
1082
+ ) -> list [str ]:
1089
1083
"""
1090
1084
>>> flatten_clean_comma_separated_arguments(["a, b ,\n c, d,", "e"])
1091
1085
['a', 'b', 'c', 'd', 'e']
@@ -1227,7 +1221,7 @@ def main(*args: str) -> int:
1227
1221
f"ERROR: cannot find dictionary file: { dictionary } " ,
1228
1222
)
1229
1223
use_dictionaries .append (dictionary )
1230
- misspellings : Dict [str , Misspelling ] = {}
1224
+ misspellings : dict [str , Misspelling ] = {}
1231
1225
for dictionary in use_dictionaries :
1232
1226
build_dict (dictionary , misspellings , ignore_words )
1233
1227
colors = TermColors ()
@@ -1255,7 +1249,7 @@ def main(*args: str) -> int:
1255
1249
context_after = max (0 , options .after_context )
1256
1250
context = (context_before , context_after )
1257
1251
1258
- exclude_lines : Set [str ] = set ()
1252
+ exclude_lines : set [str ] = set ()
1259
1253
if options .exclude_file :
1260
1254
exclude_files = flatten_clean_comma_separated_arguments (options .exclude_file )
1261
1255
for exclude_file in exclude_files :
0 commit comments