Skip to content

Commit ef41a9f

Browse files
authored
Remove venv for stubtest stdlib (#10259)
1 parent cf25698 commit ef41a9f

File tree

2 files changed

+46
-64
lines changed

2 files changed

+46
-64
lines changed

stdlib/distutils/__init__.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Attempts to improve these stubs are probably not the best use of time:
2+
# - distutils is deleted in Python 3.12 and newer
3+
# - Most users already do not use stdlib distutils, due to setuptools monkeypatching
4+
# - We have very little quality assurance on these stubs, since due to the two above issues
5+
# we allowlist all distutils errors in stubtest.

tests/stubtest_stdlib.py

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,79 +10,56 @@
1010

1111
import subprocess
1212
import sys
13-
import tempfile
1413
from pathlib import Path
1514

16-
from utils import get_mypy_req, make_venv, print_error
17-
1815

1916
def run_stubtest(typeshed_dir: Path) -> int:
2017
allowlist_dir = typeshed_dir / "tests" / "stubtest_allowlists"
2118
version_allowlist = f"py{sys.version_info.major}{sys.version_info.minor}.txt"
2219
platform_allowlist = f"{sys.platform}.txt"
2320
combined_allowlist = f"{sys.platform}-py{sys.version_info.major}{sys.version_info.minor}.txt"
24-
with tempfile.TemporaryDirectory() as tmp:
25-
venv_dir = Path(tmp)
26-
print("Setting up an isolated virtual environment...", file=sys.stderr)
27-
try:
28-
pip_exe, python_exe = make_venv(venv_dir)
29-
except Exception:
30-
print_error("fail")
31-
raise
32-
33-
# Install the same mypy version as in "requirements-tests.txt"
34-
try:
35-
install_command = [pip_exe, "install", get_mypy_req()]
36-
subprocess.run(install_command, check=True, text=True, capture_output=True)
37-
except subprocess.CalledProcessError as e:
38-
print(e.stderr, file=sys.stderr)
39-
raise
40-
41-
# Uninstall setuptools from the venv so we can test stdlib's distutils
42-
try:
43-
uninstall_command = [pip_exe, "uninstall", "-y", "setuptools"]
44-
subprocess.run(uninstall_command, check=True, text=True, capture_output=True)
45-
except subprocess.CalledProcessError as e:
46-
print(e.stderr, file=sys.stderr)
47-
raise
4821

49-
cmd = [
50-
python_exe,
51-
"-m",
52-
"mypy.stubtest",
53-
"--check-typeshed",
54-
"--custom-typeshed-dir",
55-
str(typeshed_dir),
56-
"--allowlist",
57-
str(allowlist_dir / "py3_common.txt"),
58-
"--allowlist",
59-
str(allowlist_dir / version_allowlist),
60-
]
61-
if (allowlist_dir / platform_allowlist).exists():
62-
cmd += ["--allowlist", str(allowlist_dir / platform_allowlist)]
63-
if (allowlist_dir / combined_allowlist).exists():
64-
cmd += ["--allowlist", str(allowlist_dir / combined_allowlist)]
65-
if sys.version_info < (3, 10):
66-
# As discussed in https://github.com/python/typeshed/issues/3693, we only aim for
67-
# positional-only arg accuracy for python 3.10 and above.
68-
cmd += ["--ignore-positional-only"]
69-
print(" ".join(cmd), file=sys.stderr)
70-
try:
71-
subprocess.run(cmd, check=True)
72-
except subprocess.CalledProcessError as e:
73-
print(
74-
"\nNB: stubtest output depends on the Python version (and system) it is run with. "
75-
+ "See README.md for more details.\n"
76-
+ "NB: We only check positional-only arg accuracy for Python 3.10.\n"
77-
+ f"\nCommand run was: {' '.join(cmd)}\n",
78-
file=sys.stderr,
79-
)
80-
print("\n\n", file=sys.stderr)
81-
print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_dir}', file=sys.stderr)
82-
return e.returncode
83-
else:
84-
print("stubtest succeeded", file=sys.stderr)
85-
return 0
22+
# Note when stubtest imports distutils, it will likely actually import setuptools._distutils
23+
# This is fine because we don't care about distutils and allowlist all errors from it
24+
# https://github.com/python/typeshed/pull/10253#discussion_r1216712404
25+
# https://github.com/python/typeshed/pull/9734
26+
cmd = [
27+
sys.executable,
28+
"-m",
29+
"mypy.stubtest",
30+
"--check-typeshed",
31+
"--custom-typeshed-dir",
32+
str(typeshed_dir),
33+
"--allowlist",
34+
str(allowlist_dir / "py3_common.txt"),
35+
"--allowlist",
36+
str(allowlist_dir / version_allowlist),
37+
]
38+
if (allowlist_dir / platform_allowlist).exists():
39+
cmd += ["--allowlist", str(allowlist_dir / platform_allowlist)]
40+
if (allowlist_dir / combined_allowlist).exists():
41+
cmd += ["--allowlist", str(allowlist_dir / combined_allowlist)]
42+
if sys.version_info < (3, 10):
43+
# As discussed in https://github.com/python/typeshed/issues/3693, we only aim for
44+
# positional-only arg accuracy for python 3.10 and above.
45+
cmd += ["--ignore-positional-only"]
46+
print(" ".join(cmd), file=sys.stderr)
47+
try:
48+
subprocess.run(cmd, check=True)
49+
except subprocess.CalledProcessError as e:
50+
print(
51+
"\nNB: stubtest output depends on the Python version (and system) it is run with. "
52+
+ "See README.md for more details.\n"
53+
+ "NB: We only check positional-only arg accuracy for Python 3.10.\n"
54+
+ f"\nCommand run was: {' '.join(cmd)}\n",
55+
file=sys.stderr,
56+
)
57+
print("\n\n", file=sys.stderr)
58+
print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_dir}', file=sys.stderr)
59+
return e.returncode
60+
else:
61+
print("stubtest succeeded", file=sys.stderr)
62+
return 0
8663

8764

8865
if __name__ == "__main__":

0 commit comments

Comments
 (0)