Skip to content

Commit 22a495d

Browse files
committed
gh-133403: Add type annotations to generate_levenshtein_examples.py
1 parent daa9aa4 commit 22a495d

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

.github/workflows/mypy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ on:
1919
- "Tools/build/consts_getter.py"
2020
- "Tools/build/deepfreeze.py"
2121
- "Tools/build/generate-build-details.py"
22+
- "Tools/build/generate_levenshtein_examples.py"
2223
- "Tools/build/generate_sbom.py"
2324
- "Tools/build/generate_stdlib_module_names.py"
2425
- "Tools/build/mypy.ini"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add type annotations to :file:`Tools/build/generate_levenshtein_examples.py`
2+
and configure mypy to check it with strict type checking.

Tools/build/generate_levenshtein_examples.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
_CASE_COST = 1
1414

1515

16-
def _substitution_cost(ch_a, ch_b):
16+
def _substitution_cost(ch_a: str, ch_b: str) -> int:
1717
if ch_a == ch_b:
1818
return 0
1919
if ch_a.lower() == ch_b.lower():
@@ -22,7 +22,7 @@ def _substitution_cost(ch_a, ch_b):
2222

2323

2424
@lru_cache(None)
25-
def levenshtein(a, b):
25+
def levenshtein(a: str, b: str) -> int:
2626
if not a or not b:
2727
return (len(a) + len(b)) * _MOVE_COST
2828
option1 = levenshtein(a[:-1], b[:-1]) + _substitution_cost(a[-1], b[-1])
@@ -31,7 +31,7 @@ def levenshtein(a, b):
3131
return min(option1, option2, option3)
3232

3333

34-
def main():
34+
def main() -> None:
3535
parser = argparse.ArgumentParser(description=__doc__)
3636
parser.add_argument('output_path', metavar='FILE', type=str)
3737
parser.add_argument('--overwrite', dest='overwrite', action='store_const',
@@ -48,7 +48,7 @@ def main():
4848
)
4949
return
5050

51-
examples = set()
51+
examples: set[tuple[str, str, int]] = set()
5252
# Create a lot of non-empty examples, which should end up with a Gauss-like
5353
# distribution for even costs (moves) and odd costs (case substitutions).
5454
while len(examples) < 9990:

Tools/build/mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ files =
99
Tools/build/consts_getter.py,
1010
Tools/build/deepfreeze.py,
1111
Tools/build/generate-build-details.py,
12+
Tools/build/generate_levenshtein_examples.py,
1213
Tools/build/generate_sbom.py,
1314
Tools/build/generate_stdlib_module_names.py,
1415
Tools/build/verify_ensurepip_wheels.py,

0 commit comments

Comments
 (0)