Skip to content

Commit e17eb63

Browse files
committed
✨ Add stable_only policy to gen_str_catalog.py
Problem: - If someone wants to be ultra-safe, every string should be in `stable_strings.json`. Solution: - Add a typo policy that requires every string from the code to be found in `stable_strings.json`.
1 parent e21c5e2 commit e17eb63

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

tools/gen_str_catalog.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ def typo_fix_quiet(s: str, stable: str, i: int) -> str:
118118
}
119119

120120

121-
def handle_typo(stable_ids: dict, s: str, d: int, fn, gen) -> str:
121+
def handle_typo(stable_ids: dict, s: str, d: int, behavior: str, gen) -> str:
122+
if behavior == "stable_only":
123+
raise Exception(f"Error (using policy stable_only): \"{s}\" not found in stable strings.")
122124
if d != 0:
123125
from Levenshtein import distance
124126
for (i, value) in stable_ids.values():
125127
if distance(s, value) <= d:
126-
if fn(s, value, i) == value:
128+
if typo_behavior[behavior](s, value, i) == value:
127129
return i
128130
return next(gen)
129131

@@ -148,7 +150,7 @@ def get_id(stable_ids, key_fn, string_fn, gen, obj):
148150
if key in stable_ids:
149151
return stable_ids[key][0]
150152
else:
151-
return handle_typo(stable_ids, string_fn(obj), typo_distance, typo_behavior[typo_detect], gen)
153+
return handle_typo(stable_ids, string_fn(obj), typo_distance, typo_detect, gen)
152154

153155
stable_msg_ids, stable_module_ids = stable_ids
154156

@@ -454,9 +456,9 @@ def parse_cmdline():
454456
parser.add_argument(
455457
"--typo_detect",
456458
type=str,
457-
choices=["error", "warn", "fix", "fix_quiet"],
459+
choices=["stable_only", "error", "warn", "fix", "fix_quiet"],
458460
default="error",
459-
help="What to do when detecting a typo against stable strings.",
461+
help="Policy to handle detecting a typo against stable strings.",
460462
)
461463
parser.add_argument(
462464
"--module_id_max",

0 commit comments

Comments
 (0)