Skip to content

Commit 2bab3ae

Browse files
Avasammingyu.park
authored andcommitted
Support requires_python in runtests.py (python#14051)
1 parent 2c457a3 commit 2bab3ae

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/ts_utils/metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _is_nested_dict(obj: object) -> TypeGuard[dict[str, dict[str, Any]]]:
4747

4848

4949
@functools.cache
50-
def _get_oldest_supported_python() -> str:
50+
def get_oldest_supported_python() -> str:
5151
with PYPROJECT_PATH.open("rb") as config:
5252
val = tomli.load(config)["tool"]["typeshed"]["oldest_supported_python"]
5353
assert type(val) is str
@@ -276,7 +276,7 @@ def read_metadata(distribution: str) -> StubMetadata:
276276
partial_stub: object = data.get("partial_stub", True)
277277
assert type(partial_stub) is bool
278278
requires_python_str: object = data.get("requires_python")
279-
oldest_supported_python = _get_oldest_supported_python()
279+
oldest_supported_python = get_oldest_supported_python()
280280
oldest_supported_python_specifier = Specifier(f">={oldest_supported_python}")
281281
if requires_python_str is None:
282282
requires_python = oldest_supported_python_specifier

tests/runtests.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from importlib.util import find_spec
1010
from pathlib import Path
1111

12+
from ts_utils.metadata import get_oldest_supported_python, read_metadata
1213
from ts_utils.paths import TEST_CASES_DIR, test_cases_path
1314
from ts_utils.utils import colored
1415

@@ -19,9 +20,6 @@
1920
_SUCCESS = colored("Success", "green")
2021
_SKIPPED = colored("Skipped", "yellow")
2122
_FAILED = colored("Failed", "red")
22-
# We're using the oldest fully supported version because it's the most likely to produce errors
23-
# due to unsupported syntax, feature, or bug in a tool.
24-
_PYTHON_VERSION = "3.9"
2523

2624

2725
def _parse_jsonc(json_text: str) -> str:
@@ -52,15 +50,16 @@ def main() -> None:
5250
)
5351
parser.add_argument(
5452
"--python-version",
55-
default=_PYTHON_VERSION,
53+
default=None,
5654
choices=("3.9", "3.10", "3.11", "3.12", "3.13", "3.14"),
57-
help="Target Python version for the test (default: %(default)s).",
55+
# We're using the oldest fully supported version because it's the most likely to produce errors
56+
# due to unsupported syntax, feature, or bug in a tool.
57+
help="Target Python version for the test (defaults to oldest supported Python version).",
5858
)
5959
parser.add_argument("path", help="Path of the stub to test in format <folder>/<stub>, from the root of the project.")
6060
args = parser.parse_args()
6161
path = Path(args.path)
6262
run_stubtest: bool = args.run_stubtest
63-
python_version: str = args.python_version
6463

6564
if len(path.parts) != 2:
6665
parser.error("'path' argument should be in format <folder>/<stub>.")
@@ -69,6 +68,14 @@ def main() -> None:
6968
parser.error("Only the 'stdlib' and 'stubs' folders are supported.")
7069
if not path.exists():
7170
parser.error(f"{path=} does not exist.")
71+
72+
if args.python_version:
73+
python_version: str = args.python_version
74+
elif folder in "stubs":
75+
python_version = read_metadata(stub).requires_python.version
76+
else:
77+
python_version = get_oldest_supported_python()
78+
7279
stubtest_result: subprocess.CompletedProcess[bytes] | None = None
7380
pytype_result: subprocess.CompletedProcess[bytes] | None = None
7481

0 commit comments

Comments
 (0)