From e7294515d5f424548197bb0f40478fa48f6fc368 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 13 May 2025 12:20:16 -0400 Subject: [PATCH] Support `requires_python` in `runtests.py` --- lib/ts_utils/metadata.py | 4 ++-- tests/runtests.py | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/ts_utils/metadata.py b/lib/ts_utils/metadata.py index 2cf093ffc4a4..da1a15af0f30 100644 --- a/lib/ts_utils/metadata.py +++ b/lib/ts_utils/metadata.py @@ -47,7 +47,7 @@ def _is_nested_dict(obj: object) -> TypeGuard[dict[str, dict[str, Any]]]: @functools.cache -def _get_oldest_supported_python() -> str: +def get_oldest_supported_python() -> str: with PYPROJECT_PATH.open("rb") as config: val = tomli.load(config)["tool"]["typeshed"]["oldest_supported_python"] assert type(val) is str @@ -276,7 +276,7 @@ def read_metadata(distribution: str) -> StubMetadata: partial_stub: object = data.get("partial_stub", True) assert type(partial_stub) is bool requires_python_str: object = data.get("requires_python") - oldest_supported_python = _get_oldest_supported_python() + oldest_supported_python = get_oldest_supported_python() oldest_supported_python_specifier = Specifier(f">={oldest_supported_python}") if requires_python_str is None: requires_python = oldest_supported_python_specifier diff --git a/tests/runtests.py b/tests/runtests.py index 64346f4e58fd..efffc84df747 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -9,6 +9,7 @@ from importlib.util import find_spec from pathlib import Path +from ts_utils.metadata import get_oldest_supported_python, read_metadata from ts_utils.paths import TEST_CASES_DIR, test_cases_path from ts_utils.utils import colored @@ -19,9 +20,6 @@ _SUCCESS = colored("Success", "green") _SKIPPED = colored("Skipped", "yellow") _FAILED = colored("Failed", "red") -# We're using the oldest fully supported version because it's the most likely to produce errors -# due to unsupported syntax, feature, or bug in a tool. -_PYTHON_VERSION = "3.9" def _parse_jsonc(json_text: str) -> str: @@ -52,15 +50,16 @@ def main() -> None: ) parser.add_argument( "--python-version", - default=_PYTHON_VERSION, + default=None, choices=("3.9", "3.10", "3.11", "3.12", "3.13", "3.14"), - help="Target Python version for the test (default: %(default)s).", + # We're using the oldest fully supported version because it's the most likely to produce errors + # due to unsupported syntax, feature, or bug in a tool. + help="Target Python version for the test (defaults to oldest supported Python version).", ) parser.add_argument("path", help="Path of the stub to test in format /, from the root of the project.") args = parser.parse_args() path = Path(args.path) run_stubtest: bool = args.run_stubtest - python_version: str = args.python_version if len(path.parts) != 2: parser.error("'path' argument should be in format /.") @@ -69,6 +68,14 @@ def main() -> None: parser.error("Only the 'stdlib' and 'stubs' folders are supported.") if not path.exists(): parser.error(f"{path=} does not exist.") + + if args.python_version: + python_version: str = args.python_version + elif folder in "stubs": + python_version = read_metadata(stub).requires_python.version + else: + python_version = get_oldest_supported_python() + stubtest_result: subprocess.CompletedProcess[bytes] | None = None pytype_result: subprocess.CompletedProcess[bytes] | None = None