11import importlib
2+ import io
23import os
34import subprocess
45import sys
89from 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 ,
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
0 commit comments