From fa332220b7f223df8f72da3863100194d8312f83 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 13 Mar 2022 06:01:14 -0700 Subject: [PATCH] Fix mypy test (#7478) * Fix `mypy_test.py` so that it actually tests the third-party stubs * Upgrade to mypy 0.940 * Skip running mypy on `SQLAlchemy` stubs for now, to workaround a mypy crash. Because we didn't set mypy's clean_exit parameter, it was exiting immediately after checking the stdlib, meaning `mypy_test.py` wasn't checking the third-party stubs at all. Co-authored-by: Alex Waygood --- requirements-tests.txt | 2 +- tests/mypy_test.py | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/requirements-tests.txt b/requirements-tests.txt index f2de12f8a..f615a84d4 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -1,4 +1,4 @@ -mypy==0.931 +mypy==0.940 pytype==2022.2.23; platform_system != "Windows" and python_version < "3.10" # must match .pre-commit-config.yaml black==22.1.0 diff --git a/tests/mypy_test.py b/tests/mypy_test.py index ae4d0e249..70a73597f 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -171,7 +171,7 @@ def add_configuration(configurations: list[MypyDistConf], distribution: str) -> def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False): try: - from mypy.main import main as mypy_main + from mypy.api import run as mypy_run except ImportError: print("Cannot import mypy. Did you install it?") sys.exit(1) @@ -185,15 +185,16 @@ def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False temp.flush() flags = get_mypy_flags(args, major, minor, temp.name, custom_typeshed=custom_typeshed) - sys.argv = ["mypy"] + flags + files + mypy_args = [*flags, *files] if args.verbose: - print("running", " ".join(sys.argv)) - if not args.dry_run: - try: - mypy_main("", sys.stdout, sys.stderr) - except SystemExit as err: - return err.code - return 0 + print("running mypy", " ".join(mypy_args)) + if args.dry_run: + exit_code = 0 + else: + stdout, stderr, exit_code = mypy_run(mypy_args) + print(stdout, end="") + print(stderr, file=sys.stderr, end="") + return exit_code def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_typeshed: bool = False) -> list[str]: @@ -231,7 +232,7 @@ def read_dependencies(distribution: str) -> list[str]: for dependency in requires: assert isinstance(dependency, str) assert dependency.startswith("types-") - dependencies.append(dependency[6:]) + dependencies.append(dependency[6:].split("<")[0]) return dependencies @@ -323,6 +324,9 @@ def main(): # Test files of all third party distributions. print("Running mypy " + " ".join(get_mypy_flags(args, major, minor, "/tmp/..."))) for distribution in sorted(os.listdir("stubs")): + if distribution == "SQLAlchemy": + continue # Crashes + if not is_supported(distribution, major): continue