Skip to content

Commit a2c3fc0

Browse files
selimbmkurnikov
authored andcommitted
Make it work on Windows (#14)
* make it work on Windows. ditch capturer. * fix test for mypy==0.750
1 parent da1aa55 commit a2c3fc0

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

pytest_mypy/collect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import tempfile
12
from typing import Any, Dict, List
23

34
import pytest
@@ -96,7 +97,7 @@ def pytest_collect_file(path, parent):
9697

9798
def pytest_addoption(parser: Parser) -> None:
9899
group = parser.getgroup('mypy-tests')
99-
group.addoption('--mypy-testing-base', type=str, default='/tmp',
100+
group.addoption('--mypy-testing-base', type=str, default=tempfile.gettempdir(),
100101
help='Base directory for tests to use')
101102
group.addoption('--mypy-ini-file', type=str,
102103
help='Which .ini file to use as a default config for tests')

pytest_mypy/item.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pathlib import Path
88
from typing import Any, Dict, List, Tuple, Callable, Optional
99

10-
import capturer
1110
import pytest
1211
from _pytest._code import ExceptionInfo
1312
from _pytest._code.code import ReprEntry, ReprFileLocation
@@ -17,7 +16,12 @@
1716
from mypy.main import process_options
1817
from pytest_mypy import utils
1918
from pytest_mypy.collect import File, YamlTestFile
20-
from pytest_mypy.utils import TypecheckAssertionError, assert_string_arrays_equal, fname_to_module
19+
from pytest_mypy.utils import (
20+
TypecheckAssertionError,
21+
capture_std_streams,
22+
assert_string_arrays_equal,
23+
fname_to_module,
24+
)
2125

2226

2327
class TraceLastReprEntry(ReprEntry):
@@ -163,6 +167,9 @@ def typecheck_in_new_subprocess(self, execution_path: Path, mypy_cmd_options: Li
163167

164168
# add current directory to path
165169
self.environment_variables['PYTHONPATH'] = str(execution_path)
170+
# Windows requires this to be set, otherwise the interpreter crashes
171+
if 'SYSTEMROOT' in os.environ:
172+
self.environment_variables['SYSTEMROOT'] = os.environ['SYSTEMROOT']
166173
completed = subprocess.run([mypy_executable, *mypy_cmd_options],
167174
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
168175
cwd=os.getcwd(),
@@ -180,12 +187,10 @@ def typecheck_in_same_process(self, execution_path: Path, mypy_cmd_options: List
180187
# add current directory to path
181188
sys.path.insert(0, str(execution_path))
182189

183-
with capturer.CaptureOutput(merged=False) as captured_std_streams:
190+
with capture_std_streams() as (stdout, stderr):
184191
return_code = run_mypy_typechecking(mypy_cmd_options)
185192

186-
stdout = captured_std_streams.stdout.get_text()
187-
stderr = captured_std_streams.stderr.get_text()
188-
return return_code, (stdout, stderr)
193+
return return_code, (stdout.getvalue(), stderr.getvalue())
189194

190195
def execute_extension_hook(self) -> None:
191196
extension_hook_fqname = self.config.option.mypy_extension_hook

pytest_mypy/tests/test-extension.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# if hook works, main should contain 'reveal_type(1)'
44
reveal_type: 1
55
out: |
6-
main:1: note: Revealed type is 'builtins.int'
6+
main:1: note: Revealed type is 'Literal[1]?'
77

pytest_mypy/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Borrowed from Pew.
22
# See https://github.com/berdario/pew/blob/master/pew/_utils.py#L82
3+
import contextlib
34
import inspect
5+
import io
46
import os
57
import re
68
import sys
79
from pathlib import Path
8-
from typing import Callable, List, Optional, Tuple
10+
from typing import Callable, Iterator, List, Optional, Tuple
911

1012
from decorator import contextmanager
1113

@@ -310,3 +312,15 @@ def cd(path):
310312
yield
311313
finally:
312314
os.chdir(prev_cwd)
315+
316+
317+
@contextmanager
318+
def capture_std_streams() -> Iterator[Tuple[io.StringIO, io.StringIO]]:
319+
"""Context manager to temporarily capture stdout and stderr.
320+
321+
Returns ``(stdout, stderr)``.
322+
"""
323+
out = io.StringIO()
324+
err = io.StringIO()
325+
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
326+
yield out, err

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
'pytest',
88
'mypy>=0.730',
99
'decorator',
10-
'capturer',
1110
'pyyaml'
1211
]
1312

0 commit comments

Comments
 (0)