Skip to content

Commit c5cfc29

Browse files
committed
gh-133403: Add type annotations to generate_levenshtein_examples.py
Added type annotations to Tools/build/generate_levenshtein_examples.py and configured mypy to check it with strict type checking. Updated both Tools/build/mypy.ini and .github/workflows/mypy.yml to include the file.
1 parent 1e78f7e commit c5cfc29

File tree

3 files changed

+8
-4
lines changed

3 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"

Tools/build/generate_levenshtein_examples.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Generate 10,000 unique examples for the Levenshtein short-circuit tests."""
22

3+
from __future__ import annotations
4+
35
import argparse
46
import json
57
import os.path
@@ -13,7 +15,7 @@
1315
_CASE_COST = 1
1416

1517

16-
def _substitution_cost(ch_a, ch_b):
18+
def _substitution_cost(ch_a: str, ch_b: str) -> int:
1719
if ch_a == ch_b:
1820
return 0
1921
if ch_a.lower() == ch_b.lower():
@@ -22,7 +24,7 @@ def _substitution_cost(ch_a, ch_b):
2224

2325

2426
@lru_cache(None)
25-
def levenshtein(a, b):
27+
def levenshtein(a: str, b: str) -> int:
2628
if not a or not b:
2729
return (len(a) + len(b)) * _MOVE_COST
2830
option1 = levenshtein(a[:-1], b[:-1]) + _substitution_cost(a[-1], b[-1])
@@ -31,7 +33,7 @@ def levenshtein(a, b):
3133
return min(option1, option2, option3)
3234

3335

34-
def main():
36+
def main() -> None:
3537
parser = argparse.ArgumentParser(description=__doc__)
3638
parser.add_argument('output_path', metavar='FILE', type=str)
3739
parser.add_argument('--overwrite', dest='overwrite', action='store_const',
@@ -48,7 +50,7 @@ def main():
4850
)
4951
return
5052

51-
examples = set()
53+
examples: set[tuple[str, str, int]] = set()
5254
# Create a lot of non-empty examples, which should end up with a Gauss-like
5355
# distribution for even costs (moves) and odd costs (case substitutions).
5456
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)