diff --git a/.github/workflows/typing-coverage-comment.yml b/.github/workflows/typing-coverage-comment.yml new file mode 100644 index 000000000..5e6fda93f --- /dev/null +++ b/.github/workflows/typing-coverage-comment.yml @@ -0,0 +1,91 @@ +name: Typing coverage comment + +on: + workflow_run: + workflows: ["Typing coverage"] + types: [completed] + +permissions: + contents: read + pull-requests: write + +concurrency: + group: ${{ github.workflow }}-${{ github.event.workflow_run.id }} + cancel-in-progress: false + +jobs: + comment: + name: Post typing coverage report + runs-on: ubuntu-latest + if: >- + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion != 'cancelled' + steps: + - name: Download typing coverage report + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: typing-coverage-report + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + path: report + + - name: Create or update sticky PR comment + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const fs = require("fs"); + const path = require("path"); + + const prNumberPath = path.join("report", "pr-number.txt"); + const commentPath = path.join("report", "comment.md"); + if (!fs.existsSync(prNumberPath) || !fs.existsSync(commentPath)) { + core.info("Typing coverage report artifact missing; skipping PR comment."); + return; + } + + const issueNumber = Number.parseInt( + fs.readFileSync(prNumberPath, "utf8").trim(), + 10 + ); + if (!Number.isInteger(issueNumber) || issueNumber <= 0) { + core.setFailed(`Invalid PR number in artifact: ${issueNumber}`); + return; + } + + const body = fs.readFileSync(commentPath, "utf8").trim(); + const marker = ""; + if (!body.startsWith(marker)) { + core.setFailed("Report artifact is missing expected comment marker."); + return; + } + + const { owner, repo } = context.repo; + const comments = await github.paginate( + github.rest.issues.listComments, + { owner, repo, issue_number: issueNumber, per_page: 100 } + ); + const existing = comments.find( + (c) => + c.user && + c.user.type === "Bot" && + typeof c.body === "string" && + c.body.includes(marker) + ); + + if (existing) { + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: existing.id, + body, + }); + core.info(`Updated typing coverage comment #${existing.id} on PR #${issueNumber}.`); + } else { + const created = await github.rest.issues.createComment({ + owner, + repo, + issue_number: issueNumber, + body, + }); + core.info(`Created typing coverage comment #${created.data.id} on PR #${issueNumber}.`); + } diff --git a/.github/workflows/typing-coverage.yml b/.github/workflows/typing-coverage.yml new file mode 100644 index 000000000..f27b4bb0b --- /dev/null +++ b/.github/workflows/typing-coverage.yml @@ -0,0 +1,88 @@ +name: Typing coverage + +on: + pull_request: + types: [opened, synchronize, reopened, labeled, unlabeled] + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + typing-coverage: + name: Typing coverage + runs-on: ubuntu-latest + env: + MYPY_VERSION: "2.4.1" + COVERAGE_THRESHOLD: "50.0" + MAX_DROP_PP: "5.0" + OVERRIDE_LABEL: "allow-typing-regression" + steps: + - name: Check out PR head + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + path: pr + + - name: Check out base branch + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.base.sha }} + path: base + + - name: Set up Python + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: "3.13" + + - name: Install mypy + run: python -m pip install --no-cache-dir "mypy==${MYPY_VERSION}" + + - name: Compare typing coverage + id: compare + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + BASE_REF: ${{ github.event.pull_request.base.ref }} + PR_SHA: ${{ github.event.pull_request.head.sha }} + HAS_OVERRIDE_LABEL: ${{ contains(github.event.pull_request.labels.*.name, 'allow-typing-regression') }} + run: | + mkdir -p report + printf '%s\n' "${PR_NUMBER}" > report/pr-number.txt + + extra_args=() + if [ "${HAS_OVERRIDE_LABEL}" = "true" ]; then + extra_args+=(--allow-regression) + fi + + status=0 + python pr/bin/typing-coverage.py \ + --base-dir base \ + --pr-dir pr \ + --package rope \ + --threshold "${COVERAGE_THRESHOLD}" \ + --max-drop "${MAX_DROP_PP}" \ + --override-label "${OVERRIDE_LABEL}" \ + --base-ref "${BASE_REF}" \ + --pr-sha "${PR_SHA}" \ + --markdown-out report/comment.md \ + --json-out report/results.json \ + "${extra_args[@]}" || status=$? + + if [ -f report/comment.md ]; then + cat report/comment.md >> "${GITHUB_STEP_SUMMARY}" + fi + exit "${status}" + + - name: Upload typing coverage report + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: typing-coverage-report + path: | + report/pr-number.txt + report/comment.md + report/results.json + retention-days: 7 + if-no-files-found: warn diff --git a/bin/typing-coverage.py b/bin/typing-coverage.py new file mode 100755 index 000000000..ec3db4597 --- /dev/null +++ b/bin/typing-coverage.py @@ -0,0 +1,583 @@ +#!/usr/bin/env python3 +"""Compare mypy typing coverage between a base branch and a PR branch. + +Runs mypy with ``--lineprecision-report`` and ``--linecount-report`` (or reads +pre-generated report directories) for both revisions, produces a Markdown +report suitable for a GitHub PR comment / step summary, and evaluates the +project's two-phase typing coverage policy: + +1. While ``base`` precise-line coverage is below ``--threshold`` (default 50%), + the check is strictly informational and exits 0. +2. Once ``base`` precise-line coverage reaches ``--threshold``, a regression + greater than ``--max-drop`` percentage points (default 5.0 pp) fails the + check (exit 1) unless ``--allow-regression`` is passed (for example via the + ``allow-typing-regression`` PR label). +""" + +from __future__ import annotations + +import argparse +import dataclasses +import html +import json +import subprocess +import sys +import tempfile +from pathlib import Path +from typing import Mapping + +COMMENT_MARKER = "" + + +@dataclasses.dataclass(frozen=True) +class ModulePrecision: + lines: int + precise: int + imprecise: int + any_lines: int + empty: int + unanalyzed: int + + @property + def non_empty(self) -> int: + return self.lines - self.empty + + @property + def precision_pct(self) -> float: + if self.non_empty <= 0: + return 100.0 + return 100.0 * self.precise / self.non_empty + + +@dataclasses.dataclass(frozen=True) +class ModuleFuncCount: + annotated_funcs: int + total_funcs: int + + @property + def annotated_pct(self) -> float: + if self.total_funcs <= 0: + return 100.0 + return 100.0 * self.annotated_funcs / self.total_funcs + + +@dataclasses.dataclass(frozen=True) +class CoverageSnapshot: + precision_by_module: dict[str, ModulePrecision] + funcs_by_module: dict[str, ModuleFuncCount] + + @property + def precise_lines(self) -> int: + return sum(m.precise for m in self.precision_by_module.values()) + + @property + def non_empty_lines(self) -> int: + return sum(m.non_empty for m in self.precision_by_module.values()) + + @property + def precision_pct(self) -> float: + if self.non_empty_lines <= 0: + return 0.0 + return 100.0 * self.precise_lines / self.non_empty_lines + + @property + def annotated_funcs(self) -> int: + return sum(m.annotated_funcs for m in self.funcs_by_module.values()) + + @property + def total_funcs(self) -> int: + return sum(m.total_funcs for m in self.funcs_by_module.values()) + + @property + def annotated_funcs_pct(self) -> float: + if self.total_funcs <= 0: + return 0.0 + return 100.0 * self.annotated_funcs / self.total_funcs + + +@dataclasses.dataclass(frozen=True) +class PolicyEvaluation: + enforced: bool + passed: bool + bypassed: bool + delta_pp: float + status_label: str + summary_line: str + + +def parse_lineprecision(path: Path) -> dict[str, ModulePrecision]: + """Parse mypy's ``lineprecision.txt`` report.""" + modules: dict[str, ModulePrecision] = {} + for line in path.read_text(encoding="utf-8").splitlines(): + stripped = line.strip() + if not stripped or stripped.startswith("Name ") or set(stripped) == {"-"}: + continue + parts = stripped.split() + if len(parts) != 7: + continue + name = parts[0] + lines, precise, imprecise, any_lines, empty, unanalyzed = ( + int(v) for v in parts[1:] + ) + modules[name] = ModulePrecision( + lines=lines, + precise=precise, + imprecise=imprecise, + any_lines=any_lines, + empty=empty, + unanalyzed=unanalyzed, + ) + return modules + + +def parse_linecount(path: Path) -> dict[str, ModuleFuncCount]: + """Parse mypy's ``linecount.txt`` report.""" + modules: dict[str, ModuleFuncCount] = {} + for line in path.read_text(encoding="utf-8").splitlines(): + stripped = line.strip() + if not stripped: + continue + parts = stripped.split() + if len(parts) != 5: + continue + name = parts[4] + if name == "total": + continue + annotated_funcs = int(parts[2]) + total_funcs = int(parts[3]) + modules[name] = ModuleFuncCount( + annotated_funcs=annotated_funcs, + total_funcs=total_funcs, + ) + return modules + + +def run_mypy_snapshot(source_root: Path, package: str) -> CoverageSnapshot: + """Run mypy in ``source_root`` and return a parsed coverage snapshot.""" + with tempfile.TemporaryDirectory(prefix="rope-mypy-") as tmp: + tmp_path = Path(tmp) + precision_dir = tmp_path / "lineprecision" + count_dir = tmp_path / "linecount" + cmd = [ + sys.executable, + "-m", + "mypy", + "--ignore-missing-imports", + "--no-error-summary", + "--no-incremental", + "--cache-dir=/dev/null", + "--lineprecision-report", + str(precision_dir), + "--linecount-report", + str(count_dir), + package, + ] + # mypy exits non-zero when type errors exist, which is expected on a + # partially-typed codebase; we still require both report files to exist. + proc = subprocess.run( + cmd, + cwd=source_root, + capture_output=True, + text=True, + check=False, + ) + precision_file = precision_dir / "lineprecision.txt" + count_file = count_dir / "linecount.txt" + if not precision_file.is_file() or not count_file.is_file(): + raise RuntimeError( + f"mypy did not produce expected reports in {source_root} " + f"(exit {proc.returncode}).\nstdout:\n{proc.stdout}\n" + f"stderr:\n{proc.stderr}" + ) + return CoverageSnapshot( + precision_by_module=parse_lineprecision(precision_file), + funcs_by_module=parse_linecount(count_file), + ) + + +def load_snapshot_from_dir(report_dir: Path) -> CoverageSnapshot: + """Load a snapshot from a directory containing ``lineprecision.txt`` and ``linecount.txt``.""" + return CoverageSnapshot( + precision_by_module=parse_lineprecision(report_dir / "lineprecision.txt"), + funcs_by_module=parse_linecount(report_dir / "linecount.txt"), + ) + + +def evaluate_policy( + base: CoverageSnapshot, + pr: CoverageSnapshot, + threshold: float, + max_drop: float, + allow_regression: bool, + override_label: str, +) -> PolicyEvaluation: + """Evaluate the two-phase typing coverage policy.""" + delta_pp = pr.precision_pct - base.precision_pct + enforced = base.precision_pct >= threshold + + if not enforced: + return PolicyEvaluation( + enforced=False, + passed=True, + bypassed=False, + delta_pp=delta_pp, + status_label="Informational", + summary_line=( + f"Informational only: base coverage ({base.precision_pct:.2f}%) " + f"is below the {threshold:.0f}% enforcement threshold." + ), + ) + + if delta_pp < -max_drop: + if allow_regression: + return PolicyEvaluation( + enforced=True, + passed=True, + bypassed=True, + delta_pp=delta_pp, + status_label="Approved regression", + summary_line=( + f"Coverage dropped by {abs(delta_pp):.2f} pp " + f"(exceeding {max_drop:.1f} pp), explicitly approved via " + f"the `{override_label}` label." + ), + ) + return PolicyEvaluation( + enforced=True, + passed=False, + bypassed=False, + delta_pp=delta_pp, + status_label="Regression", + summary_line=( + f"Coverage dropped by {abs(delta_pp):.2f} pp " + f"(maximum allowed without approval: {max_drop:.1f} pp). " + f"Add type annotations or request maintainer approval with the " + f"`{override_label}` label." + ), + ) + + return PolicyEvaluation( + enforced=True, + passed=True, + bypassed=False, + delta_pp=delta_pp, + status_label="Passed", + summary_line=( + f"Within policy ({delta_pp:+.2f} pp; maximum allowed drop without " + f"approval is {max_drop:.1f} pp)." + ), + ) + + +def _format_pp(delta: float) -> str: + if abs(delta) < 0.005: + return "0.00 pp" + return f"{delta:+.2f} pp" + + +def _changed_modules( + base: Mapping[str, ModulePrecision], + pr: Mapping[str, ModulePrecision], +) -> list[tuple[str, ModulePrecision | None, ModulePrecision | None, float]]: + rows: list[tuple[str, ModulePrecision | None, ModulePrecision | None, float]] = [] + for name in sorted(set(base) | set(pr)): + b = base.get(name) + p = pr.get(name) + if b == p: + continue + b_pct = b.precision_pct if b is not None else 0.0 + p_pct = p.precision_pct if p is not None else 0.0 + delta = p_pct - b_pct + rows.append((name, b, p, delta)) + rows.sort(key=lambda item: (item[3], item[0])) + return rows + + +def render_markdown( + base: CoverageSnapshot, + pr: CoverageSnapshot, + evaluation: PolicyEvaluation, + threshold: float, + max_drop: float, + override_label: str, + base_ref: str, + pr_sha: str, + max_module_rows: int = 25, +) -> str: + """Render a concise HTML/Markdown report for PR comments and step summaries.""" + func_delta_pp = pr.annotated_funcs_pct - base.annotated_funcs_pct + if not evaluation.passed: + badge = "❌" + elif evaluation.bypassed: + badge = "⚠️" + elif evaluation.delta_pp >= 0.005: + badge = "✨" + elif evaluation.delta_pp <= -0.005: + badge = "πŸ“‰" + else: + badge = "βœ…" + + progress = ( + min(100.0, 100.0 * pr.precision_pct / threshold) if threshold > 0 else 100.0 + ) + filled_blocks = round(progress / 5) + progress_bar = "β–ˆ" * filled_blocks + "β–‘" * (20 - filled_blocks) + + escaped_base_ref = html.escape(base_ref) + escaped_pr_sha = html.escape(pr_sha[:12]) if pr_sha else "PR" + + lines = [ + COMMENT_MARKER, + f"### {badge} Typing Coverage Report ({evaluation.status_label})", + "", + evaluation.summary_line, + "", + "", + " ", + " ", + " ", + f" ", + f" ", + " ", + " ", + " ", + " ", + " ", + " ", + f" ", + f" ", + f" ", + " ", + " ", + " ", + f" ", + f" ", + f" ", + " ", + " ", + "
MetricBase ({escaped_base_ref})PR ({escaped_pr_sha})Delta
Precise lines (primary){base.precision_pct:.2f}% ({base.precise_lines:,} / {base.non_empty_lines:,}){pr.precision_pct:.2f}% ({pr.precise_lines:,} / {pr.non_empty_lines:,}){_format_pp(evaluation.delta_pp)}
Annotated functions{base.annotated_funcs_pct:.2f}% ({base.annotated_funcs:,} / {base.total_funcs:,}){pr.annotated_funcs_pct:.2f}% ({pr.annotated_funcs:,} / {pr.total_funcs:,}){_format_pp(func_delta_pp)}
", + "", + ] + + if not evaluation.enforced: + lines.append( + f"**Progress toward {threshold:.0f}% enforcement threshold:** " + f"`{progress_bar}` {pr.precision_pct:.2f}% / {threshold:.0f}%" + ) + lines.append("") + + changed = _changed_modules(base.precision_by_module, pr.precision_by_module) + if changed: + lines.extend( + [ + f"
Changed modules ({len(changed)})", + "", + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + ] + ) + for name, b, p, delta in changed[:max_module_rows]: + b_cell = ( + f"{b.precision_pct:.2f}% ({b.precise}/{b.non_empty})" + if b is not None + else "new" + ) + p_cell = ( + f"{p.precision_pct:.2f}% ({p.precise}/{p.non_empty})" + if p is not None + else "removed" + ) + delta_cell = _format_pp(delta) if (b is not None and p is not None) else "β€”" + lines.extend( + [ + " ", + f" ", + f" ", + f" ", + f" ", + " ", + ] + ) + lines.extend([" ", "
ModuleBasePRDelta
{html.escape(name)}{b_cell}{p_cell}{delta_cell}
"]) + if len(changed) > max_module_rows: + lines.append("") + lines.append( + f"…and {len(changed) - max_module_rows} more changed modules." + ) + lines.extend(["
", ""]) + else: + lines.append("_No per-module typing coverage changes detected._") + lines.append("") + + lines.append( + f"Measured with mypy --lineprecision-report and " + f"--linecount-report. Policy: informational below " + f"{threshold:.0f}% precise lines; above {threshold:.0f}%, drops greater " + f"than {max_drop:.1f} pp require the {html.escape(override_label)} " + f"label from a maintainer." + ) + return "\n".join(lines) + "\n" + + +def build_arg_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + description="Compare mypy typing coverage between base and PR checkouts." + ) + source_group = parser.add_mutually_exclusive_group(required=True) + source_group.add_argument( + "--base-dir", + type=Path, + help="Path to base branch checkout (requires --pr-dir).", + ) + source_group.add_argument( + "--base-report-dir", + type=Path, + help="Path to pre-generated base mypy report dir (requires --pr-report-dir).", + ) + parser.add_argument( + "--pr-dir", + type=Path, + help="Path to PR branch checkout (used with --base-dir).", + ) + parser.add_argument( + "--pr-report-dir", + type=Path, + help="Path to pre-generated PR mypy report dir (used with --base-report-dir).", + ) + parser.add_argument( + "--package", + default="rope", + help="Package directory to pass to mypy (default: rope).", + ) + parser.add_argument( + "--threshold", + type=float, + default=50.0, + help="Enforcement threshold in percent of precise lines (default: 50.0).", + ) + parser.add_argument( + "--max-drop", + type=float, + default=5.0, + help="Maximum allowed drop in percentage points once enforced (default: 5.0).", + ) + parser.add_argument( + "--allow-regression", + action="store_true", + help="Allow regressions exceeding --max-drop (maintainer override).", + ) + parser.add_argument( + "--override-label", + default="allow-typing-regression", + help="PR label name documented for maintainer approval.", + ) + parser.add_argument( + "--base-ref", + default="master", + help="Base branch name for display.", + ) + parser.add_argument( + "--pr-sha", + default="", + help="PR head commit SHA for display.", + ) + parser.add_argument( + "--markdown-out", + type=Path, + help="Write the rendered Markdown/HTML report to this path.", + ) + parser.add_argument( + "--json-out", + type=Path, + help="Write structured coverage results as JSON to this path.", + ) + return parser + + +def main(argv: list[str] | None = None) -> int: + parser = build_arg_parser() + args = parser.parse_args(argv) + + if args.base_dir is not None: + if args.pr_dir is None: + parser.error("--pr-dir is required when --base-dir is provided") + base_snapshot = run_mypy_snapshot(args.base_dir, args.package) + pr_snapshot = run_mypy_snapshot(args.pr_dir, args.package) + else: + if args.pr_report_dir is None: + parser.error( + "--pr-report-dir is required when --base-report-dir is provided" + ) + base_snapshot = load_snapshot_from_dir(args.base_report_dir) + pr_snapshot = load_snapshot_from_dir(args.pr_report_dir) + + evaluation = evaluate_policy( + base=base_snapshot, + pr=pr_snapshot, + threshold=args.threshold, + max_drop=args.max_drop, + allow_regression=args.allow_regression, + override_label=args.override_label, + ) + + markdown = render_markdown( + base=base_snapshot, + pr=pr_snapshot, + evaluation=evaluation, + threshold=args.threshold, + max_drop=args.max_drop, + override_label=args.override_label, + base_ref=args.base_ref, + pr_sha=args.pr_sha, + ) + + if args.markdown_out is not None: + args.markdown_out.parent.mkdir(parents=True, exist_ok=True) + args.markdown_out.write_text(markdown, encoding="utf-8") + else: + sys.stdout.write(markdown) + + if args.json_out is not None: + payload = { + "base": { + "precise_lines": base_snapshot.precise_lines, + "non_empty_lines": base_snapshot.non_empty_lines, + "precision_pct": round(base_snapshot.precision_pct, 4), + "annotated_funcs": base_snapshot.annotated_funcs, + "total_funcs": base_snapshot.total_funcs, + "annotated_funcs_pct": round(base_snapshot.annotated_funcs_pct, 4), + }, + "pr": { + "precise_lines": pr_snapshot.precise_lines, + "non_empty_lines": pr_snapshot.non_empty_lines, + "precision_pct": round(pr_snapshot.precision_pct, 4), + "annotated_funcs": pr_snapshot.annotated_funcs, + "total_funcs": pr_snapshot.total_funcs, + "annotated_funcs_pct": round(pr_snapshot.annotated_funcs_pct, 4), + }, + "policy": { + "threshold": args.threshold, + "max_drop": args.max_drop, + "enforced": evaluation.enforced, + "passed": evaluation.passed, + "bypassed": evaluation.bypassed, + "delta_pp": round(evaluation.delta_pp, 4), + "status_label": evaluation.status_label, + }, + } + args.json_out.parent.mkdir(parents=True, exist_ok=True) + args.json_out.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8") + + return 0 if evaluation.passed else 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/docs/contributing.rst b/docs/contributing.rst index 796699986..d2b568c07 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -144,6 +144,26 @@ All required packages for development could be installed with:: pip install -e ".[dev]" +Typing Coverage +--------------- + +Rope tracks static typing coverage on pull requests using ``mypy``'s +``--lineprecision-report`` (precise non-empty lines, primary metric) and +``--linecount-report`` (annotated functions, secondary metric), comparing the +PR against its base commit: + +* **Below 50% precise-line coverage:** the check is strictly informational. It + posts and updates a summary comment on the pull request showing the coverage + change and per-module breakdown, and always passes. +* **At or above 50% precise-line coverage:** a drop of more than **5 percentage + points** fails the check unless a maintainer explicitly approves the + regression by applying the ``allow-typing-regression`` label to the pull + request. + +You can run the same comparison locally against two checkouts:: + + python3 bin/typing-coverage.py --base-dir /path/to/base --pr-dir . + .. _GitHub: http://github.com/ .. _`python-rope/rope`: https://github.com/python-rope/rope .. _`pull request`: https://help.github.com/articles/using-pull-requests