From ce3ce7f78ed75ba1cb75ec95d76b9db4173a4a1b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 12 Mar 2022 18:39:21 -0800 Subject: [PATCH 1/2] Fix mypy test Because we didn't set mypy's clean_exit parameter, it was exiting immediately after checking the stdlib. This exposed two issues: - types-requests has a version-limited dependency on types-urllib3, which we crashed on - mypy crashes checking the SQLAlchemy stubs. Not sure if that's a known issue. --- requirements-tests.txt | 2 +- tests/mypy_test.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/requirements-tests.txt b/requirements-tests.txt index f2de12f8a49e..f615a84d4aa4 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 ae4d0e249376..921a8c67e224 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -190,7 +190,7 @@ def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False print("running", " ".join(sys.argv)) if not args.dry_run: try: - mypy_main("", sys.stdout, sys.stderr) + mypy_main("", sys.stdout, sys.stderr, clean_exit=True) except SystemExit as err: return err.code return 0 @@ -231,7 +231,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 +323,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 From 8322791f2932febe3284528d5b20fc0f2d8abe20 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 12 Mar 2022 19:29:05 -0800 Subject: [PATCH 2/2] use mypy api --- tests/mypy_test.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 921a8c67e224..70a73597f8ac 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, clean_exit=True) - 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]: