Skip to content

Commit 1c6f433

Browse files
author
Foster Guo
committed
feat: add type checking for scripts and enhance project configuration
1 parent f7f1f90 commit 1c6f433

8 files changed

Lines changed: 36 additions & 10 deletions

File tree

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ repos:
4646
files: ^matcher_py/.*\.py$
4747
pass_filenames: false
4848

49+
- id: ty-check-scripts
50+
name: ty check scripts
51+
entry: bash -c 'cd matcher_rs/scripts && uv run ty check'
52+
language: system
53+
files: ^matcher_rs/scripts/.*\.py$
54+
pass_filenames: false
55+
4956
# matcher_java: clippy + checkstyle
5057
- id: clippy-matcher-java
5158
name: clippy matcher_java

Justfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fmt-check:
2727

2828
# -- Lint ----------------------------------------------------------------------
2929

30-
lint: lint-rs lint-py lint-java lint-c
30+
lint: lint-rs lint-py lint-java lint-c lint-scripts
3131
cargo all-features clippy --workspace --all-targets -- -D warnings
3232
cargo doc --workspace --all-features --no-deps
3333

@@ -59,6 +59,10 @@ lint-c:
5959
cargo fmt --all
6060
cargo clippy -- -D warnings
6161

62+
[working-directory: 'matcher_rs/scripts']
63+
lint-scripts:
64+
uv run ty check
65+
6266
# -- Test ----------------------------------------------------------------------
6367

6468
test: test-rs test-py test-java test-c

matcher_py/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ dev = ["pytest", "pip", "maturin"]
4040
[tool.uv]
4141
cache-keys = [{ file = "matcher_py.pyi"}, { file = "**/*.rs"}, { file = "**/*.toml"}, { file = "../matcher_rs/**/*.rs"}, { file = "../matcher_rs/**/*.toml"} ]
4242

43+
[tool.ty.environment]
44+
python = ".venv"
45+
4346
[tool.maturin]
4447
bindings = "pyo3"
4548
module-name = "matcher_py"

matcher_rs/scripts/bench_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ def compare_result_maps(
141141
baseline: dict[str, float],
142142
candidate: dict[str, float],
143143
min_change_pct: float,
144-
) -> tuple[list[dict[str, object]], list[dict[str, object]], set[str], set[str]]:
145-
regressions: list[dict[str, object]] = []
146-
improvements: list[dict[str, object]] = []
144+
) -> tuple[list[dict[str, str | int | float]], list[dict[str, str | int | float]], set[str], set[str]]:
145+
regressions: list[dict[str, str | int | float]] = []
146+
improvements: list[dict[str, str | int | float]] = []
147147

148148
shared_paths = sorted(set(baseline) & set(candidate))
149149
baseline_only = set(baseline) - set(candidate)
@@ -174,7 +174,7 @@ def compare_result_maps(
174174
return regressions, improvements, baseline_only, candidate_only
175175

176176

177-
def print_change_section(title: str, rows: Iterable[dict[str, object]]) -> None:
177+
def print_change_section(title: str, rows: Iterable[dict[str, str | int | float]]) -> None:
178178
rows = list(rows)
179179
print(title)
180180
if not rows:

matcher_rs/scripts/generate_process_map.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ def iter_scalar_chars() -> list[str]:
216216
]
217217

218218

219-
def format_numeric_value(value: float) -> str:
219+
def format_numeric_value(value: int | float) -> str:
220220
if not math.isfinite(value):
221221
raise ValueError(f"unsupported numeric value: {value!r}")
222-
if value.is_integer():
222+
if isinstance(value, int) or value.is_integer():
223223
return str(int(value))
224224

225225
fraction = Fraction(value).limit_denominator(MAX_FRACTION_DENOMINATOR)
@@ -514,15 +514,15 @@ def build_emoji_norm_map() -> dict[str, str]:
514514
# ---------------------------------------------------------------------------
515515

516516
def render_mapping(mapping: dict[str, str]) -> str:
517-
lines = [f"{key}\t{mapping[key]}" for key in sorted(mapping, key=ord)]
517+
lines = [f"{key}\t{mapping[key]}" for key in sorted(mapping)]
518518
return "\n".join(lines) + "\n"
519519

520520

521521
def render_codepoints(codepoints: list[int]) -> str:
522522
return "\n".join(f"U+{codepoint:04X}" for codepoint in sorted(codepoints)) + "\n"
523523

524524

525-
def collect_outputs(root: Path) -> tuple[dict[Path, str], dict[str, object]]:
525+
def collect_outputs(root: Path) -> tuple[dict[Path, str], dict[str, str | dict[str, str] | list[str] | dict[str, int]]]:
526526
chars = iter_scalar_chars()
527527
process_map_dir = root / "matcher_rs" / "process_map"
528528

matcher_rs/scripts/pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "matcher-scripts"
3+
version = "0.0.0"
4+
requires-python = ">=3.10"
5+
6+
[dependency-groups]
7+
dev = ["plotly", "opencc", "pypinyin", "requests"]
8+
9+
[tool.ty.environment]
10+
python = ".venv"

matcher_rs/scripts/visualize_dispatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def make_winner_heatmap(rows: list[dict], mode: str, pat_cjk: int) -> go.Figure:
144144
margin_row.append(0)
145145
hover_row.append("")
146146
continue
147-
best_engine = max(throughputs, key=throughputs.get)
147+
best_engine = max(throughputs, key=lambda k: throughputs[k])
148148
best_val = throughputs[best_engine]
149149
second_val = max(v for e, v in throughputs.items() if e != best_engine) if len(throughputs) > 1 else best_val
150150
margin = ((best_val - second_val) / second_val * 100) if second_val > 0 else 0

ty.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[environment]
2+
root = ["matcher_py", "matcher_rs/scripts"]

0 commit comments

Comments
 (0)