Files
SolitaryThinker 21e59268d8 [misc] v2: full pre-commit clean (ruff UP038/SIM/UP031 + mypy annotations)
Sweep all of v2/ through pre-commit (was previously only run on changed
files). Fixes surfaced across untouched modules:

- ruff: isinstance-tuple -> X | Y (UP038), try/except/pass ->
  contextlib.suppress (SIM105), negated-return (SIM103), %-format ->
  f-string (UP031).
- mypy: add annotations for no-untyped-call + var-annotated across recipes,
  training methods, torch/toy backends, and serving.
- yapf reflow of the SF-Wan KV-cache call sites (semantics unchanged).

yapf/ruff/codespell/mypy all pass; v2 tests 237 passed / 5 pre-existing
(bucket_c torch-absent on CPU venv).
2026-06-19 22:16:28 +00:00

68 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Zero-dependency test runner for v2.
The v2 core is numpy-only and CPU-testable. This runner discovers
``test_*`` functions in ``v2/tests/test_*.py`` and runs them without
needing pytest installed (it is fully pytest-compatible, so ``pytest`` works too).
Usage:
python3 v2/run_tests.py # run all
python3 v2/run_tests.py interleave # run files matching a substring
"""
from __future__ import annotations
import importlib.util
import sys
import traceback
from pathlib import Path
HERE = Path(__file__).resolve().parent
TESTS_DIR = HERE / "tests"
REPO_ROOT = HERE.parent # so `import v2` works
def _load(path: Path):
spec = importlib.util.spec_from_file_location(f"_mftest_{path.stem}", path)
assert spec and spec.loader
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def main(argv: list[str]) -> int:
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
needle = argv[0] if argv else ""
files = sorted(p for p in TESTS_DIR.glob("test_*.py") if needle in p.name)
if not files:
print(f"no test files matching {needle!r} under {TESTS_DIR}")
return 1
passed = failed = 0
failures: list[str] = []
for f in files:
mod = _load(f)
fns = [(n, getattr(mod, n)) for n in sorted(dir(mod)) if n.startswith("test_") and callable(getattr(mod, n))]
for name, fn in fns:
label = f"{f.name}::{name}"
try:
fn()
passed += 1
print(f" PASS {label}")
except Exception: # noqa: BLE001 - test runner reports everything
failed += 1
tb = traceback.format_exc()
failures.append(f"{label}\n{tb}")
print(f" FAIL {label}")
print(f"\n{passed} passed, {failed} failed")
if failures:
print("\n" + "=" * 70 + "\nFAILURES\n" + "=" * 70)
for fail in failures:
print("\n" + fail)
return 0 if failed == 0 else 1
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))