Skip to content

Commit fa33222

Browse files
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 <[email protected]>
1 parent 3e3cc2a commit fa33222

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

requirements-tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mypy==0.931
1+
mypy==0.940
22
pytype==2022.2.23; platform_system != "Windows" and python_version < "3.10"
33
# must match .pre-commit-config.yaml
44
black==22.1.0

tests/mypy_test.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def add_configuration(configurations: list[MypyDistConf], distribution: str) ->
171171

172172
def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False):
173173
try:
174-
from mypy.main import main as mypy_main
174+
from mypy.api import run as mypy_run
175175
except ImportError:
176176
print("Cannot import mypy. Did you install it?")
177177
sys.exit(1)
@@ -185,15 +185,16 @@ def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False
185185
temp.flush()
186186

187187
flags = get_mypy_flags(args, major, minor, temp.name, custom_typeshed=custom_typeshed)
188-
sys.argv = ["mypy"] + flags + files
188+
mypy_args = [*flags, *files]
189189
if args.verbose:
190-
print("running", " ".join(sys.argv))
191-
if not args.dry_run:
192-
try:
193-
mypy_main("", sys.stdout, sys.stderr)
194-
except SystemExit as err:
195-
return err.code
196-
return 0
190+
print("running mypy", " ".join(mypy_args))
191+
if args.dry_run:
192+
exit_code = 0
193+
else:
194+
stdout, stderr, exit_code = mypy_run(mypy_args)
195+
print(stdout, end="")
196+
print(stderr, file=sys.stderr, end="")
197+
return exit_code
197198

198199

199200
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]:
231232
for dependency in requires:
232233
assert isinstance(dependency, str)
233234
assert dependency.startswith("types-")
234-
dependencies.append(dependency[6:])
235+
dependencies.append(dependency[6:].split("<")[0])
235236
return dependencies
236237

237238

@@ -323,6 +324,9 @@ def main():
323324
# Test files of all third party distributions.
324325
print("Running mypy " + " ".join(get_mypy_flags(args, major, minor, "/tmp/...")))
325326
for distribution in sorted(os.listdir("stubs")):
327+
if distribution == "SQLAlchemy":
328+
continue # Crashes
329+
326330
if not is_supported(distribution, major):
327331
continue
328332

0 commit comments

Comments
 (0)