Skip to content

Commit 4389341

Browse files
emmatypingilevkivskyi
authored andcommitted
Run each PEP561 test in its virtualenv dir (#5225)
When working on partial stub packages I realized that the changes in #5060 were flaking. This change runs mypy in each temporary directory, which avoids parrallelism/cache issues (sometimes the multiple processes would try to read/write the same cache file). To test this thoroughly, I ran the test 25 times in a row, with no failures.
1 parent 29889c8 commit 4389341

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

mypy/test/testpep561.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from contextlib import contextmanager
22
import os
3-
import random
4-
import shutil
5-
import string
63
import sys
74
import tempfile
8-
from typing import Iterator, List, Generator
5+
from typing import Tuple, List, Generator, Optional
96
from unittest import TestCase, main
107

118
import mypy.api
@@ -25,30 +22,40 @@ def check_mypy_run(cmd_line: List[str],
2522
python_executable: str = sys.executable,
2623
expected_out: str = '',
2724
expected_err: str = '',
28-
expected_returncode: int = 1) -> None:
25+
expected_returncode: int = 1,
26+
venv_dir: Optional[str] = None) -> None:
2927
"""Helper to run mypy and check the output."""
30-
if python_executable != sys.executable:
31-
cmd_line.append('--python-executable={}'.format(python_executable))
32-
out, err, returncode = mypy.api.run(cmd_line)
33-
assert out == expected_out, err
34-
assert err == expected_err, out
35-
assert returncode == expected_returncode, returncode
28+
if venv_dir is not None:
29+
old_dir = os.getcwd()
30+
os.chdir(venv_dir)
31+
try:
32+
if python_executable != sys.executable:
33+
cmd_line.append('--python-executable={}'.format(python_executable))
34+
out, err, returncode = mypy.api.run(cmd_line)
35+
assert out == expected_out, err
36+
assert err == expected_err, out
37+
assert returncode == expected_returncode, returncode
38+
finally:
39+
if venv_dir is not None:
40+
os.chdir(old_dir)
3641

3742

3843
class TestPEP561(TestCase):
3944

4045
@contextmanager
41-
def virtualenv(self, python_executable: str = sys.executable) -> Generator[str, None, None]:
46+
def virtualenv(self,
47+
python_executable: str = sys.executable
48+
) -> Generator[Tuple[str, str], None, None]:
4249
"""Context manager that creates a virtualenv in a temporary directory
4350
4451
returns the path to the created Python executable"""
4552
with tempfile.TemporaryDirectory() as venv_dir:
4653
run_command([sys.executable, '-m', 'virtualenv', '-p{}'.format(python_executable),
4754
venv_dir], cwd=os.getcwd())
4855
if sys.platform == 'win32':
49-
yield os.path.abspath(os.path.join(venv_dir, 'Scripts', 'python'))
56+
yield venv_dir, os.path.abspath(os.path.join(venv_dir, 'Scripts', 'python'))
5057
else:
51-
yield os.path.abspath(os.path.join(venv_dir, 'bin', 'python'))
58+
yield venv_dir, os.path.abspath(os.path.join(venv_dir, 'bin', 'python'))
5259

5360
def install_package(self, pkg: str,
5461
python_executable: str = sys.executable) -> None:
@@ -78,53 +85,63 @@ def test_get_pkg_dirs(self) -> None:
7885
assert dirs
7986

8087
def test_typedpkg_stub_package(self) -> None:
81-
with self.virtualenv() as python_executable:
88+
with self.virtualenv() as venv:
89+
venv_dir, python_executable = venv
8290
self.install_package('typedpkg-stubs', python_executable)
8391
check_mypy_run(
8492
[self.tempfile],
8593
python_executable,
86-
self.msg_list,
94+
expected_out=self.msg_list,
95+
venv_dir=venv_dir,
8796
)
8897

8998
def test_typedpkg(self) -> None:
90-
with self.virtualenv() as python_executable:
99+
with self.virtualenv() as venv:
100+
venv_dir, python_executable = venv
91101
self.install_package('typedpkg', python_executable)
92102
check_mypy_run(
93103
[self.tempfile],
94104
python_executable,
95-
self.msg_tuple,
105+
expected_out=self.msg_tuple,
106+
venv_dir=venv_dir,
96107
)
97108

98109
def test_stub_and_typed_pkg(self) -> None:
99-
with self.virtualenv() as python_executable:
110+
with self.virtualenv() as venv:
111+
venv_dir, python_executable = venv
100112
self.install_package('typedpkg', python_executable)
101113
self.install_package('typedpkg-stubs', python_executable)
102114
check_mypy_run(
103115
[self.tempfile],
104116
python_executable,
105-
self.msg_list,
117+
expected_out=self.msg_list,
118+
venv_dir=venv_dir,
106119
)
107120

108121
def test_typedpkg_stubs_python2(self) -> None:
109122
python2 = try_find_python2_interpreter()
110123
if python2:
111-
with self.virtualenv(python2) as py2:
124+
with self.virtualenv(python2) as venv:
125+
venv_dir, py2 = venv
112126
self.install_package('typedpkg-stubs', py2)
113127
check_mypy_run(
114128
[self.tempfile],
115129
py2,
116-
self.msg_list,
130+
expected_out=self.msg_list,
131+
venv_dir=venv_dir,
117132
)
118133

119134
def test_typedpkg_python2(self) -> None:
120135
python2 = try_find_python2_interpreter()
121136
if python2:
122-
with self.virtualenv(python2) as py2:
137+
with self.virtualenv(python2) as venv:
138+
venv_dir, py2 = venv
123139
self.install_package('typedpkg', py2)
124140
check_mypy_run(
125141
[self.tempfile],
126142
py2,
127-
self.msg_tuple,
143+
expected_out=self.msg_tuple,
144+
venv_dir=venv_dir,
128145
)
129146

130147

0 commit comments

Comments
 (0)