|
10 | 10 |
|
11 | 11 | import subprocess |
12 | 12 | import sys |
13 | | -import tempfile |
14 | 13 | from pathlib import Path |
15 | 14 |
|
16 | | -from utils import get_mypy_req, make_venv, print_error |
17 | | - |
18 | 15 |
|
19 | 16 | def run_stubtest(typeshed_dir: Path) -> int: |
20 | 17 | allowlist_dir = typeshed_dir / "tests" / "stubtest_allowlists" |
21 | 18 | version_allowlist = f"py{sys.version_info.major}{sys.version_info.minor}.txt" |
22 | 19 | platform_allowlist = f"{sys.platform}.txt" |
23 | 20 | 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 |
48 | 21 |
|
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 |
86 | 63 |
|
87 | 64 |
|
88 | 65 | if __name__ == "__main__": |
|
0 commit comments