Skip to content

Commit 71ff2d7

Browse files
authored
Improve output capture from mypy when running in same process (#108)
1 parent 38543f8 commit 71ff2d7

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

pytest_mypy_plugins/item.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
import io
23
import os
34
import subprocess
45
import sys
@@ -8,10 +9,10 @@
89
from typing import (
910
TYPE_CHECKING,
1011
Any,
11-
Callable,
1212
Dict,
1313
List,
1414
Optional,
15+
TextIO,
1516
Tuple,
1617
Union,
1718
no_type_check,
@@ -36,7 +37,6 @@
3637
OutputMatcher,
3738
TypecheckAssertionError,
3839
assert_expected_matched_actual,
39-
capture_std_streams,
4040
fname_to_module,
4141
)
4242

@@ -87,15 +87,15 @@ class ReturnCodes:
8787
FATAL_ERROR = 2
8888

8989

90-
def run_mypy_typechecking(cmd_options: List[str]) -> Optional[Union[str, int]]:
90+
def run_mypy_typechecking(cmd_options: List[str], stdout: TextIO, stderr: TextIO) -> Optional[Union[str, int]]:
9191
fscache = FileSystemCache()
9292
sources, options = process_options(cmd_options, fscache=fscache)
9393

9494
error_messages = []
9595

9696
def flush_errors(new_messages: List[str], serious: bool) -> None:
9797
error_messages.extend(new_messages)
98-
f = sys.stderr if serious else sys.stdout
98+
f = stderr if serious else stdout
9999
try:
100100
for msg in new_messages:
101101
f.write(msg + "\n")
@@ -104,7 +104,7 @@ def flush_errors(new_messages: List[str], serious: bool) -> None:
104104
sys.exit(ReturnCodes.FATAL_ERROR)
105105

106106
try:
107-
build.build(sources, options, flush_errors=flush_errors, fscache=fscache)
107+
build.build(sources, options, flush_errors=flush_errors, fscache=fscache, stdout=stdout, stderr=stderr)
108108

109109
except SystemExit as sysexit:
110110
return sysexit.code
@@ -224,10 +224,15 @@ def typecheck_in_same_process(
224224
# add current directory to path
225225
sys.path.insert(0, str(execution_path))
226226

227-
with capture_std_streams() as (stdout, stderr):
228-
return_code = run_mypy_typechecking(mypy_cmd_options)
227+
stdout = io.StringIO()
228+
stderr = io.StringIO()
229229

230-
return return_code, (stdout.getvalue(), stderr.getvalue())
230+
with stdout, stderr:
231+
return_code = run_mypy_typechecking(mypy_cmd_options, stdout=stdout, stderr=stderr)
232+
stdout_value = stdout.getvalue()
233+
stderr_value = stderr.getvalue()
234+
235+
return return_code, (stdout_value, stderr_value)
231236

232237
def execute_extension_hook(self) -> None:
233238
extension_hook_fqname = self.config.option.mypy_extension_hook

pytest_mypy_plugins/utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,3 @@ def cd(path: Union[str, Path]) -> Iterator[None]:
380380
yield
381381
finally:
382382
os.chdir(prev_cwd)
383-
384-
385-
@contextmanager
386-
def capture_std_streams() -> Iterator[Tuple[io.StringIO, io.StringIO]]:
387-
"""Context manager to temporarily capture stdout and stderr.
388-
389-
Returns ``(stdout, stderr)``.
390-
"""
391-
out = io.StringIO()
392-
err = io.StringIO()
393-
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
394-
yield out, err

0 commit comments

Comments
 (0)