From 1b50692b793455d0e0b16f457fb7165a5faee2f7 Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Wed, 14 Oct 2026 13:56:09 +0300 Subject: [PATCH] Honor selected standard-library files in the test runner --- tests/README.md | 2 ++ tests/runtests.py | 5 +++-- tests/stubtest_stdlib.py | 10 +++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/README.md b/tests/README.md index 2df18286d043..8ef4dfaf12da 100644 --- a/tests/README.md +++ b/tests/README.md @@ -145,6 +145,8 @@ Run using This test compares the stdlib stubs against the objects at runtime. Because of this, the output depends on which version of Python and on what kind of system it is run. +To check specific modules instead of the entire standard library, pass their +import names, for example `python tests/stubtest_stdlib.py getopt`. As such, if you run this test locally, it may complain about system-specific differences (in e.g., `socket`) that the type system cannot capture or our stubtest settings in CI do not account for. If you run into this issue, consider opening a draft PR and letting CI diff --git a/tests/runtests.py b/tests/runtests.py index 3b99cc5f5a2d..30cdd8ee717b 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -92,7 +92,8 @@ def main() -> None: stubtest_result: subprocess.CompletedProcess[bytes] | None = None print("\nRunning pre-commit...") - pre_commit_result = subprocess.run(["pre-commit", "run", "--files", *path.rglob("*")], check=False) + files = [path] if path.is_file() else list(path.rglob("*")) + pre_commit_result = subprocess.run(["pre-commit", "run", "--files", *files], check=False) print("\nRunning check_typeshed_structure.py...") check_structure_result = subprocess.run([sys.executable, "tests/check_typeshed_structure.py"], check=False) @@ -154,7 +155,7 @@ def main() -> None: if mypy_result.returncode == 0: if folder == "stdlib": print("\nRunning stubtest...") - stubtest_result = subprocess.run([sys.executable, "tests/stubtest_stdlib.py", stub], check=False) + stubtest_result = subprocess.run([sys.executable, "tests/stubtest_stdlib.py", Path(stub).stem], check=False) else: if run_stubtest: print("\nRunning stubtest...") diff --git a/tests/stubtest_stdlib.py b/tests/stubtest_stdlib.py index 76a0ee9842da..438f82b53a03 100755 --- a/tests/stubtest_stdlib.py +++ b/tests/stubtest_stdlib.py @@ -11,13 +11,14 @@ import subprocess import sys +from collections.abc import Sequence from pathlib import Path from ts_utils.paths import TS_BASE_PATH, allowlists_path from ts_utils.utils import allowlist_stubtest_arguments -def run_stubtest(typeshed_dir: Path) -> int: +def run_stubtest(typeshed_dir: Path, modules: Sequence[str] = ()) -> int: # Note when stubtest imports distutils, it will likely actually import setuptools._distutils # This is fine because we don't care about distutils and allowlist all errors from it # https://github.com/python/typeshed/pull/10253#discussion_r1216712404 @@ -26,13 +27,16 @@ def run_stubtest(typeshed_dir: Path) -> int: sys.executable, "-m", "mypy.stubtest", - "--check-typeshed", + *(modules if modules else ["--check-typeshed"]), "--show-traceback", "--strict-type-check-only", "--custom-typeshed-dir", str(typeshed_dir), *allowlist_stubtest_arguments("stdlib"), ] + if modules: + # Entries for modules outside this selection are expected to be unused. + cmd.append("--ignore-unused-allowlist") print(" ".join(cmd), file=sys.stderr) try: subprocess.run(cmd, check=True) @@ -56,4 +60,4 @@ def run_stubtest(typeshed_dir: Path) -> int: if __name__ == "__main__": - sys.exit(run_stubtest(typeshed_dir=TS_BASE_PATH)) + sys.exit(run_stubtest(typeshed_dir=TS_BASE_PATH, modules=sys.argv[1:]))