diff options
| author | natsuoto <[email protected]> | 2026-07-15 11:36:23 +0530 |
|---|---|---|
| committer | natsuoto <[email protected]> | 2026-07-15 11:36:23 +0530 |
| commit | 089004d6d63c06d11e195484e767e5fe30b5cfcb (patch) | |
| tree | f42576804f0f6ae50d3d724d9e9298ed460ee788 /tests | |
| parent | b91d28b669974abdcebacf0c8c13abaa5b124f9a (diff) | |
| download | edify-089004d6d63c06d11e195484e767e5fe30b5cfcb.tar.xz edify-089004d6d63c06d11e195484e767e5fe30b5cfcb.zip | |
test(discipline): every type-checker suppression carries a code and inline reason
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/discipline/suppressions.test.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/discipline/suppressions.test.py b/tests/discipline/suppressions.test.py new file mode 100644 index 0000000..a4ad2f3 --- /dev/null +++ b/tests/discipline/suppressions.test.py @@ -0,0 +1,35 @@ +"""Discipline: every type-checker suppression must carry a rule code AND an inline reason. + +The well-formed shape is ``# type: ignore[<code>] # <reason>`` (mypy) or +``# pyright: ignore[<code>] # <reason>`` (pyright). The check recognises both +forms and rejects any bare suppression that lacks a rule code or an inline +rationale. +""" + +import re +from pathlib import Path + +_REPO_ROOT = Path(__file__).resolve().parents[2] +_EDIFY_ROOT = _REPO_ROOT / "edify" + +_WELL_FORMED = re.compile( + r"#\s*(?:type|pyright):\s*ignore\[[^\]]+\]\s+#\s*\S+.*$", +) + +_ANY_SUPPRESSION = re.compile(r"#\s*(?:type|pyright):\s*ignore\b") + + +def _collect_python_sources(root: Path) -> list[Path]: + return sorted(p for p in root.rglob("*.py") if "__pycache__" not in p.parts) + + +def test_every_suppression_carries_a_rule_code_and_a_reason(): + offenders: list[str] = [] + for path in _collect_python_sources(_EDIFY_ROOT): + for lineno, line in enumerate(path.read_text().splitlines(), start=1): + if _ANY_SUPPRESSION.search(line) and not _WELL_FORMED.search(line): + offenders.append(f"{path.relative_to(_REPO_ROOT)}:{lineno} {line.rstrip()}") + assert not offenders, ( + "type-checker suppressions must include both a rule code and an inline reason.\n" + + "\n".join(offenders) + ) |
