Files
VoiceStudio/tests/test_python_lint_gate.py
Palash Debnath 012eb41e3a ci(python): gate Ruff and ty on the rules the code already passes (#4614)
* ci(python): gate Ruff and ty on the rules the code already passes

Ruff and ty are pinned in the dev group (never installed for users: the
app syncs with --no-dev and Docker installs without groups). CI's first
Python shard and `bun run lint:py` run the same checks.

- Ruff checks runtime-bug rules: syntax, undefined names and exports,
  f-strings without fields, invalid characters, dropped asyncio tasks.
- ty fails on any rule the code passes today; the 23 rules with a backlog
  warn, so new instances of every other rule fail CI.

Fixes what the rules found:
- The setup warmup task was not referenced and could be garbage-collected
  mid-load; it is now held until it finishes.
- A perf-budget test fake called time.sleep without importing time.
- The scam guard's invisible-character class is written as escapes.
- Type-only names (httpx, Callable) are imported under TYPE_CHECKING.

* changelog: add fragment for #4614

* build(python): let lint:py sync the pinned tools before running them
2026-12-03 07:10:37 -08:00

45 lines
1.7 KiB
Python

"""CI and `bun run lint:py` run the same pinned Ruff and ty checks.
The gate only holds if the tools are declared (so `uv sync` installs them for
CI and contributors alike), pinned exactly (so both report the same findings),
and run by CI with ty failing on errors but not on the warned backlog.
"""
from __future__ import annotations
import json
import re
import tomllib
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parents[1]
PYPROJECT = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))
def _python_job_commands() -> str:
workflow = yaml.safe_load((ROOT / ".github/workflows/ci.yml").read_text(encoding="utf-8"))
steps = workflow["jobs"]["test-python"]["steps"]
return "\n".join(step.get("run", "") for step in steps)
def test_ruff_and_ty_are_pinned_dev_tools():
dev = PYPROJECT["dependency-groups"]["dev"]
for tool in ("ruff", "ty"):
pins = [dep for dep in dev if re.match(rf"{tool}\s*==\s*\d", dep)]
assert pins, f"{tool} must be pinned with == in the dev dependency group"
def test_ci_runs_the_same_checks_as_lint_py():
commands = _python_job_commands()
assert re.search(r"uv run --no-sync ruff check\b", commands)
assert re.search(r"uv run --no-sync ty check --exit-zero-on-warning\b", commands)
script = json.loads((ROOT / "package.json").read_text(encoding="utf-8"))["scripts"]["lint:py"]
assert "ruff check" in script and "ty check --exit-zero-on-warning" in script
def test_ty_backlog_only_warns_and_never_ignores():
# A rule set to "ignore" would hide new instances; the backlog may only warn.
rules = PYPROJECT["tool"]["ty"].get("rules", {})
assert set(rules.values()) <= {"warn", "error"}, rules