1
1
import importlib
2
+ import io
2
3
import os
3
4
import subprocess
4
5
import sys
8
9
from typing import (
9
10
TYPE_CHECKING ,
10
11
Any ,
11
- Callable ,
12
12
Dict ,
13
13
List ,
14
14
Optional ,
15
+ TextIO ,
15
16
Tuple ,
16
17
Union ,
17
18
no_type_check ,
36
37
OutputMatcher ,
37
38
TypecheckAssertionError ,
38
39
assert_expected_matched_actual ,
39
- capture_std_streams ,
40
40
fname_to_module ,
41
41
)
42
42
@@ -87,15 +87,15 @@ class ReturnCodes:
87
87
FATAL_ERROR = 2
88
88
89
89
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 ]]:
91
91
fscache = FileSystemCache ()
92
92
sources , options = process_options (cmd_options , fscache = fscache )
93
93
94
94
error_messages = []
95
95
96
96
def flush_errors (new_messages : List [str ], serious : bool ) -> None :
97
97
error_messages .extend (new_messages )
98
- f = sys . stderr if serious else sys . stdout
98
+ f = stderr if serious else stdout
99
99
try :
100
100
for msg in new_messages :
101
101
f .write (msg + "\n " )
@@ -104,7 +104,7 @@ def flush_errors(new_messages: List[str], serious: bool) -> None:
104
104
sys .exit (ReturnCodes .FATAL_ERROR )
105
105
106
106
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 )
108
108
109
109
except SystemExit as sysexit :
110
110
return sysexit .code
@@ -224,10 +224,15 @@ def typecheck_in_same_process(
224
224
# add current directory to path
225
225
sys .path .insert (0 , str (execution_path ))
226
226
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 ( )
229
229
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 )
231
236
232
237
def execute_extension_hook (self ) -> None :
233
238
extension_hook_fqname = self .config .option .mypy_extension_hook
0 commit comments