From ecc5a3990136933144ff75b6b03d8f3816035fbb Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 4 Jun 2023 14:14:02 -0700 Subject: [PATCH] Remove venv for stubtest stdlib Requested by Alex in #10253 --- stdlib/distutils/__init__.pyi | 5 ++ tests/stubtest_stdlib.py | 105 +++++++++++++--------------------- 2 files changed, 46 insertions(+), 64 deletions(-) diff --git a/stdlib/distutils/__init__.pyi b/stdlib/distutils/__init__.pyi index e69de29bb2d1..328a5b783441 100644 --- a/stdlib/distutils/__init__.pyi +++ b/stdlib/distutils/__init__.pyi @@ -0,0 +1,5 @@ +# Attempts to improve these stubs are probably not the best use of time: +# - distutils is deleted in Python 3.12 and newer +# - Most users already do not use stdlib distutils, due to setuptools monkeypatching +# - We have very little quality assurance on these stubs, since due to the two above issues +# we allowlist all distutils errors in stubtest. diff --git a/tests/stubtest_stdlib.py b/tests/stubtest_stdlib.py index 496e28a3745a..32c512e4e8d2 100755 --- a/tests/stubtest_stdlib.py +++ b/tests/stubtest_stdlib.py @@ -10,79 +10,56 @@ import subprocess import sys -import tempfile from pathlib import Path -from utils import get_mypy_req, make_venv, print_error - def run_stubtest(typeshed_dir: Path) -> int: allowlist_dir = typeshed_dir / "tests" / "stubtest_allowlists" version_allowlist = f"py{sys.version_info.major}{sys.version_info.minor}.txt" platform_allowlist = f"{sys.platform}.txt" combined_allowlist = f"{sys.platform}-py{sys.version_info.major}{sys.version_info.minor}.txt" - with tempfile.TemporaryDirectory() as tmp: - venv_dir = Path(tmp) - print("Setting up an isolated virtual environment...", file=sys.stderr) - try: - pip_exe, python_exe = make_venv(venv_dir) - except Exception: - print_error("fail") - raise - - # Install the same mypy version as in "requirements-tests.txt" - try: - install_command = [pip_exe, "install", get_mypy_req()] - subprocess.run(install_command, check=True, text=True, capture_output=True) - except subprocess.CalledProcessError as e: - print(e.stderr, file=sys.stderr) - raise - - # Uninstall setuptools from the venv so we can test stdlib's distutils - try: - uninstall_command = [pip_exe, "uninstall", "-y", "setuptools"] - subprocess.run(uninstall_command, check=True, text=True, capture_output=True) - except subprocess.CalledProcessError as e: - print(e.stderr, file=sys.stderr) - raise - cmd = [ - python_exe, - "-m", - "mypy.stubtest", - "--check-typeshed", - "--custom-typeshed-dir", - str(typeshed_dir), - "--allowlist", - str(allowlist_dir / "py3_common.txt"), - "--allowlist", - str(allowlist_dir / version_allowlist), - ] - if (allowlist_dir / platform_allowlist).exists(): - cmd += ["--allowlist", str(allowlist_dir / platform_allowlist)] - if (allowlist_dir / combined_allowlist).exists(): - cmd += ["--allowlist", str(allowlist_dir / combined_allowlist)] - if sys.version_info < (3, 10): - # As discussed in https://github.com/python/typeshed/issues/3693, we only aim for - # positional-only arg accuracy for python 3.10 and above. - cmd += ["--ignore-positional-only"] - print(" ".join(cmd), file=sys.stderr) - try: - subprocess.run(cmd, check=True) - except subprocess.CalledProcessError as e: - print( - "\nNB: stubtest output depends on the Python version (and system) it is run with. " - + "See README.md for more details.\n" - + "NB: We only check positional-only arg accuracy for Python 3.10.\n" - + f"\nCommand run was: {' '.join(cmd)}\n", - file=sys.stderr, - ) - print("\n\n", file=sys.stderr) - print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_dir}', file=sys.stderr) - return e.returncode - else: - print("stubtest succeeded", file=sys.stderr) - return 0 + # Note when stubtest imports distutils, it will likely actually import setuptools._distutils + # This is fine because we don't care about distutils and allowlist all errors from it + # https://github.com/python/typeshed/pull/10253#discussion_r1216712404 + # https://github.com/python/typeshed/pull/9734 + cmd = [ + sys.executable, + "-m", + "mypy.stubtest", + "--check-typeshed", + "--custom-typeshed-dir", + str(typeshed_dir), + "--allowlist", + str(allowlist_dir / "py3_common.txt"), + "--allowlist", + str(allowlist_dir / version_allowlist), + ] + if (allowlist_dir / platform_allowlist).exists(): + cmd += ["--allowlist", str(allowlist_dir / platform_allowlist)] + if (allowlist_dir / combined_allowlist).exists(): + cmd += ["--allowlist", str(allowlist_dir / combined_allowlist)] + if sys.version_info < (3, 10): + # As discussed in https://github.com/python/typeshed/issues/3693, we only aim for + # positional-only arg accuracy for python 3.10 and above. + cmd += ["--ignore-positional-only"] + print(" ".join(cmd), file=sys.stderr) + try: + subprocess.run(cmd, check=True) + except subprocess.CalledProcessError as e: + print( + "\nNB: stubtest output depends on the Python version (and system) it is run with. " + + "See README.md for more details.\n" + + "NB: We only check positional-only arg accuracy for Python 3.10.\n" + + f"\nCommand run was: {' '.join(cmd)}\n", + file=sys.stderr, + ) + print("\n\n", file=sys.stderr) + print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_dir}', file=sys.stderr) + return e.returncode + else: + print("stubtest succeeded", file=sys.stderr) + return 0 if __name__ == "__main__":