Skip to content

Commit f99d29a

Browse files
committed
Cleanup: use type hints for the spell checker
3rd party modules cause this not to run without any errors.
1 parent f1db70d commit f99d29a

File tree

1 file changed

+57
-27
lines changed

1 file changed

+57
-27
lines changed

check_source/check_spelling.py

+57-27
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@
3232
"""
3333

3434
import os
35+
import argparse
36+
37+
from typing import (
38+
Callable,
39+
Dict,
40+
Generator,
41+
List,
42+
Optional,
43+
Set,
44+
Tuple,
45+
)
46+
47+
48+
# Report: word, line, column.
49+
Report = Tuple[str, int, int]
50+
# Cache: {filepath: length, hash, reports}.
51+
CacheData = Dict[str, Tuple[int, bytes, List[Report]]]
52+
# Map word to suggestions.
53+
SuggestMap = Dict[str, str]
3554

3655
ONLY_ONCE = True
3756
USE_COLOR = True
@@ -40,7 +59,7 @@
4059
_files_visited = set()
4160

4261
# Lowercase word -> suggestion list.
43-
_suggest_map = {}
62+
_suggest_map: SuggestMap = {}
4463

4564
VERBOSE_CACHE = False
4665

@@ -64,7 +83,7 @@
6483
# -----------------------------------------------------------------------------
6584
# General Utilities
6685

67-
def hash_of_file_and_len(fp):
86+
def hash_of_file_and_len(fp: str) -> Tuple[bytes, int]:
6887
import hashlib
6988
with open(fp, 'rb') as fh:
7089
data = fh.read()
@@ -129,11 +148,11 @@ def hash_of_file_and_len(fp):
129148
re_not_newline = re.compile("[^\n]")
130149

131150

132-
def words_from_text(text):
151+
def words_from_text(text: str) -> List[Tuple[str, int]]:
133152
""" Extract words to treat as English for spell checking.
134153
"""
135154
# Replace non-newlines with white-space, so all alignment is kept.
136-
def replace_ignore(match):
155+
def replace_ignore(match: re.Match[str]) -> str:
137156
start, end = match.span()
138157
return re_not_newline.sub(" ", match.string[start:end])
139158

@@ -148,7 +167,7 @@ def replace_ignore(match):
148167
for match in re_words.finditer(text):
149168
words.append((match.group(0), match.start()))
150169

151-
def word_ok(w):
170+
def word_ok(w: str) -> bool:
152171
# Ignore all uppercase words.
153172
if w.isupper():
154173
return False
@@ -165,16 +184,16 @@ class Comment:
165184
"type",
166185
)
167186

168-
def __init__(self, file, text, line, type):
187+
def __init__(self, file: str, text: str, line: int, type: str):
169188
self.file = file
170189
self.text = text
171190
self.line = line
172191
self.type = type
173192

174-
def parse(self):
193+
def parse(self) -> List[Tuple[str, int]]:
175194
return words_from_text(self.text)
176195

177-
def line_and_column_from_comment_offset(self, pos):
196+
def line_and_column_from_comment_offset(self, pos: int) -> Tuple[int, int]:
178197
text = self.text
179198
slineno = self.line + text.count("\n", 0, pos)
180199
# Allow for -1 to be not found.
@@ -187,7 +206,7 @@ def line_and_column_from_comment_offset(self, pos):
187206
return slineno, scol
188207

189208

190-
def extract_code_strings(filepath):
209+
def extract_code_strings(filepath: str) -> Tuple[List[Comment], Set[str]]:
191210
import pygments
192211
from pygments import lexers
193212
from pygments.token import Token
@@ -219,7 +238,7 @@ def extract_code_strings(filepath):
219238
return comments, code_words
220239

221240

222-
def extract_py_comments(filepath):
241+
def extract_py_comments(filepath: str) -> Tuple[List[Comment], Set[str]]:
223242

224243
import token
225244
import tokenize
@@ -248,7 +267,7 @@ def extract_py_comments(filepath):
248267
return comments, code_words
249268

250269

251-
def extract_c_comments(filepath):
270+
def extract_c_comments(filepath: str) -> Tuple[List[Comment], Set[str]]:
252271
"""
253272
Extracts comments like this:
254273
@@ -344,7 +363,7 @@ def extract_c_comments(filepath):
344363
return comments, code_words
345364

346365

347-
def spell_check_report(filepath, report):
366+
def spell_check_report(filepath: str, report: Report) -> None:
348367
w, slineno, scol = report
349368
w_lower = w.lower()
350369

@@ -369,7 +388,10 @@ def spell_check_report(filepath, report):
369388
))
370389

371390

372-
def spell_check_file(filepath, check_type='COMMENTS'):
391+
def spell_check_file(
392+
filepath: str,
393+
check_type: str = 'COMMENTS',
394+
) -> Generator[Report, None, None]:
373395
if check_type == 'COMMENTS':
374396
if filepath.endswith(".py"):
375397
comment_list, code_words = extract_py_comments(filepath)
@@ -396,11 +418,15 @@ def spell_check_file(filepath, check_type='COMMENTS'):
396418
yield (w, slineno, scol)
397419

398420

399-
def spell_check_file_recursive(dirpath, check_type='COMMENTS', cache_data=None):
421+
def spell_check_file_recursive(
422+
dirpath: str,
423+
check_type: str = 'COMMENTS',
424+
cache_data: Optional[CacheData]=None,
425+
) -> None:
400426
import os
401427
from os.path import join, splitext
402428

403-
def source_list(path, filename_check=None):
429+
def source_list(path: str, filename_check: Optional[Callable[[str], bool]]=None) -> Generator[str, None, None]:
404430
for dirpath, dirnames, filenames in os.walk(path):
405431
# skip '.git'
406432
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
@@ -411,7 +437,7 @@ def source_list(path, filename_check=None):
411437
if filename_check is None or filename_check(filepath):
412438
yield filepath
413439

414-
def is_source(filename):
440+
def is_source(filename: str) -> bool:
415441
ext = splitext(filename)[1]
416442
return (ext in {
417443
".c",
@@ -447,22 +473,26 @@ def is_source(filename):
447473
# )
448474
#
449475

450-
def spell_cache_read(cache_filepath):
476+
def spell_cache_read(cache_filepath: str) -> Tuple[CacheData, SuggestMap]:
451477
import pickle
452-
cache_data = {}, {}
478+
cache_store: Tuple[CacheData, SuggestMap] = {}, {}
453479
if os.path.exists(cache_filepath):
454480
with open(cache_filepath, 'rb') as fh:
455-
cache_data = pickle.load(fh)
456-
return cache_data
481+
cache_store = pickle.load(fh)
482+
return cache_store
457483

458484

459-
def spell_cache_write(cache_filepath, cache_data):
485+
def spell_cache_write(cache_filepath: str, cache_store: Tuple[CacheData, SuggestMap]) -> None:
460486
import pickle
461487
with open(cache_filepath, 'wb') as fh:
462-
pickle.dump(cache_data, fh)
488+
pickle.dump(cache_store, fh)
463489

464490

465-
def spell_check_file_with_cache_support(filepath, check_type='COMMENTS', cache_data=None):
491+
def spell_check_file_with_cache_support(
492+
filepath: str,
493+
check_type: str = 'COMMENTS',
494+
cache_data: Optional[CacheData] = None,
495+
) -> Generator[Report, None, None]:
466496
"""
467497
Iterator each item is a report: (word, line_number, column_number)
468498
"""
@@ -502,8 +532,7 @@ def spell_check_file_with_cache_support(filepath, check_type='COMMENTS', cache_d
502532
# -----------------------------------------------------------------------------
503533
# Main & Argument Parsing
504534

505-
def argparse_create():
506-
import argparse
535+
def argparse_create() -> argparse.ArgumentParser:
507536

508537
# When --help or no args are given, print this help
509538
description = __doc__
@@ -543,7 +572,7 @@ def argparse_create():
543572
return parser
544573

545574

546-
def main():
575+
def main() -> None:
547576
global _suggest_map
548577

549578
import os
@@ -553,7 +582,7 @@ def main():
553582
check_type = args.extract
554583
cache_filepath = args.cache_file
555584

556-
cache_data = None
585+
cache_data: Optional[CacheData] = None
557586
if cache_filepath:
558587
cache_data, _suggest_map = spell_cache_read(cache_filepath)
559588
clear_stale_cache = True
@@ -573,6 +602,7 @@ def main():
573602
clear_stale_cache = False
574603

575604
if cache_filepath:
605+
assert(cache_data is not None)
576606
if VERBOSE_CACHE:
577607
print("Writing cache:", len(cache_data))
578608

0 commit comments

Comments
 (0)