Skip to content

Commit 74112dc

Browse files
AvasamAlexWaygood
andauthored
Add a run_stubtest flag to scripts/runtests.py (#9717)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 3fc2f27 commit 74112dc

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

scripts/runtests.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
from __future__ import annotations
33

4+
import argparse
45
import json
56
import os
67
import re
@@ -44,16 +45,28 @@ def _get_strict_params(stub_path: str) -> list[str]:
4445

4546

4647
def main() -> None:
47-
try:
48-
path = sys.argv[1]
49-
except IndexError:
50-
print("Missing path argument in format <folder>/<stub>", file=sys.stderr)
51-
sys.exit(1)
52-
assert os.path.exists(path), rf"Path {path} does not exist."
48+
parser = argparse.ArgumentParser()
49+
parser.add_argument(
50+
"--run-stubtest",
51+
action="store_true",
52+
help=(
53+
"Run stubtest for the selected package(s). Running stubtest may download and execute arbitrary code from PyPI: "
54+
"only use this option if you trust the package you are testing."
55+
),
56+
)
57+
parser.add_argument("path", help="Path of the stub to test in format <folder>/<stub>, from the root of the project.")
58+
args = parser.parse_args()
59+
path: str = args.path
60+
run_stubtest: bool = args.run_stubtest
61+
5362
path_tokens = Path(path).parts
54-
assert len(path_tokens) == 2, "Path argument should be in format <folder>/<stub>."
63+
if len(path_tokens) != 2:
64+
parser.error("'path' argument should be in format <folder>/<stub>.")
5565
folder, stub = path_tokens
56-
assert folder in {"stdlib", "stubs"}, "Only the 'stdlib' and 'stubs' folders are supported."
66+
if folder not in {"stdlib", "stubs"}:
67+
parser.error("Only the 'stdlib' and 'stubs' folders are supported.")
68+
if not os.path.exists(path):
69+
parser.error(rf"'path' {path} does not exist.")
5770
stubtest_result: subprocess.CompletedProcess[bytes] | None = None
5871
pytype_result: subprocess.CompletedProcess[bytes] | None = None
5972

@@ -99,19 +112,18 @@ def main() -> None:
99112
print("\nRunning stubtest...")
100113
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_stdlib.py", stub])
101114
else:
102-
run_stubtest_query = (
103-
f"\nRun stubtest for {stub!r} (Y/N)?\n\n"
104-
"NOTE: Running third-party stubtest involves downloading and executing arbitrary code from PyPI.\n"
105-
f"Only run stubtest if you trust the {stub!r} package.\n"
106-
)
107-
run_stubtest_answer = input(colored(run_stubtest_query, "yellow")).lower()
108-
while run_stubtest_answer not in {"yes", "no", "y", "n"}:
109-
run_stubtest_answer = input(colored("Invalid response; please try again.\n", "red")).lower()
110-
if run_stubtest_answer in {"yes", "y"}:
111-
print("\nRunning stubtest.")
115+
if run_stubtest:
116+
print("\nRunning stubtest...")
112117
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_third_party.py", stub])
113118
else:
114-
print(colored(f"\nSkipping stubtest for {stub!r}...", "yellow"))
119+
print(
120+
colored(
121+
f"\nSkipping stubtest for {stub!r}..."
122+
+ "\nNOTE: Running third-party stubtest involves downloading and executing arbitrary code from PyPI."
123+
+ f"\nOnly run stubtest if you trust the {stub!r} package.",
124+
"yellow",
125+
)
126+
)
115127
else:
116128
print(colored("\nSkipping stubtest since mypy failed.", "yellow"))
117129

tests/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ be selected. A summary of the results will be printed to the terminal.
4646
You must provide a single argument which is a path to the stubs to test, like
4747
so: `stdlib/os` or `stubs/requests`.
4848

49+
Run `python scripts/runtests.py --help` for information on the various configuration options
50+
for this script.
51+
4952
## mypy\_test.py
5053

5154
Run using:

0 commit comments

Comments
 (0)