From 593283d2402b3d36a66b9ef54c17c583e06944a4 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Fri, 27 Nov 2026 19:20:47 +0100 Subject: [PATCH] devstats: rename html.py, it shadowed stdlib Bug-class-check: devstats/tests/test_no_stdlib_shadow.py Co-Authored-By: Claude Opus 5.5 (1M context) --- devstats/cli.py | 2 +- devstats/render.py | 2 +- devstats/{html.py => report_html.py} | 0 devstats/run.sh | 2 +- devstats/tests/test_no_stdlib_shadow.py | 17 +++++++++++++++++ .../tests/{test_html.py => test_report_html.py} | 2 +- devstats/tests/test_run_sh.py | 13 ++++++++++--- 7 files changed, 31 insertions(+), 7 deletions(-) rename devstats/{html.py => report_html.py} (100%) create mode 100644 devstats/tests/test_no_stdlib_shadow.py rename devstats/tests/{test_html.py => test_report_html.py} (97%) diff --git a/devstats/cli.py b/devstats/cli.py index f74fab1..acd1662 100644 --- a/devstats/cli.py +++ b/devstats/cli.py @@ -14,7 +14,7 @@ from typing import TextIO from devstats import article as A from devstats.aggregate import DAY, RepoData, Window, dedup, window_totals from devstats.discover import Repo, discover -from devstats.html import page +from devstats.report_html import page from devstats.numstat import read_commits from devstats.refs import list_refs, resolve, tag_times from devstats.render import by_repo, document, report diff --git a/devstats/render.py b/devstats/render.py index 622f977..986b0a3 100644 --- a/devstats/render.py +++ b/devstats/render.py @@ -1,7 +1,7 @@ """Tables of kuhy beside the article, plus the optional per-repo breakdown. The `*_rows` builders return (head, rows) of plain cell strings; the markdown -here and the HTML page in `devstats.html` both render those, so the page and +here and the HTML page in `devstats.report_html` both render those, so the page and the terminal always show the same numbers. """ diff --git a/devstats/html.py b/devstats/report_html.py similarity index 100% rename from devstats/html.py rename to devstats/report_html.py diff --git a/devstats/run.sh b/devstats/run.sh index f3ba475..87a0ff1 100755 --- a/devstats/run.sh +++ b/devstats/run.sh @@ -49,7 +49,7 @@ check_python() { # Capped (2 GiB, 10 % CPU, idle IO) when kuhy's capped.sh exists, plain otherwise. run_devstats() { - local cmd=(env "PYTHONPATH=$REPO_ROOT${PYTHONPATH:+:$PYTHONPATH}" python3 -m devstats compare "$@") + local cmd=(env "PYTHONPATH=$REPO_ROOT${PYTHONPATH:+:$PYTHONPATH}" python3 -P -m devstats compare "$@") if [[ -x "$CAPPED" ]]; then "$CAPPED" "${cmd[@]}" else diff --git a/devstats/tests/test_no_stdlib_shadow.py b/devstats/tests/test_no_stdlib_shadow.py new file mode 100644 index 0000000..f2b2429 --- /dev/null +++ b/devstats/tests/test_no_stdlib_shadow.py @@ -0,0 +1,17 @@ +"""No devstats module may share a stdlib module's name. + +`python -m devstats` run from inside `devstats/` puts that dir first on +sys.path, so a `devstats/html.py` replaced the stdlib `html` for every import. +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +PACKAGE = Path(__file__).resolve().parents[1] + + +def test_no_module_shadows_the_stdlib() -> None: + clashes = sorted(p.name for p in PACKAGE.glob("*.py") if p.stem in sys.stdlib_module_names) + assert clashes == [] diff --git a/devstats/tests/test_html.py b/devstats/tests/test_report_html.py similarity index 97% rename from devstats/tests/test_html.py rename to devstats/tests/test_report_html.py index 4baf7c6..6447c9c 100644 --- a/devstats/tests/test_html.py +++ b/devstats/tests/test_report_html.py @@ -6,7 +6,7 @@ import re from html import unescape from devstats.aggregate import Window -from devstats.html import CMDR, KUHY, chart, page +from devstats.report_html import CMDR, KUHY, chart, page from devstats.render import report diff --git a/devstats/tests/test_run_sh.py b/devstats/tests/test_run_sh.py index 17404f3..cfdc16d 100644 --- a/devstats/tests/test_run_sh.py +++ b/devstats/tests/test_run_sh.py @@ -23,12 +23,12 @@ RUN_SH = Path(__file__).resolve().parents[1] / "run.sh" BASE_TOOLS = ("bash", "realpath", "dirname", "mkdir", "cat", "setsid", "env") -def run_sh(*args: str, path: str | None = None) -> subprocess.CompletedProcess[str]: - """Run run.sh from an unrelated cwd, optionally with a replaced PATH.""" +def run_sh(*args: str, path: str | None = None, cwd: Path | str = "/") -> subprocess.CompletedProcess[str]: + """Run run.sh from an unrelated cwd (or `cwd`), optionally with a replaced PATH.""" env = dict(os.environ) if path is not None: env["PATH"] = path - return subprocess.run([str(RUN_SH), *args], cwd="/", env=env, capture_output=True, text=True, + return subprocess.run([str(RUN_SH), *args], cwd=cwd, env=env, capture_output=True, text=True, check=False, timeout=50) @@ -56,6 +56,13 @@ def test_run_prints_tables_writes_report_and_no_warnings(src: Path) -> None: assert 'class="chart"' in page +def test_run_from_inside_the_package_dir(src: Path) -> None: + """`python -m` from devstats/ must not let a package module shadow the stdlib.""" + proc = run_sh("--no-open", "--root", str(src), cwd=RUN_SH.parent) + assert proc.returncode == 0, proc.stderr + assert report_path().read_text().startswith("") + + def test_empty_root_exits_2_and_writes_no_report(tmp_path: Path) -> None: empty = tmp_path / "empty" empty.mkdir()