Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions mypy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
Any pretty formatting is left to the caller.

The 'run_dmypy' function is similar, but instead mimics invocation of
dmypy.
dmypy. Note that run_dmypy is not thread-safe and modifies sys.stdout
and sys.stderr during its invocation.

Note that these APIs don't support incremental generation of error
messages.
Expand All @@ -42,6 +43,8 @@

"""

import sys

from io import StringIO
from typing import List, Tuple, TextIO, Callable

Expand Down Expand Up @@ -69,4 +72,20 @@ def run(args: List[str]) -> Tuple[str, str, int]:

def run_dmypy(args: List[str]) -> Tuple[str, str, int]:
from mypy.dmypy.client import main
return _run(lambda stdout, stderr: main(args))

# A bunch of effort has been put into threading stdout and stderr
# through the main API to avoid the threadsafety problems of
# modifying sys.stdout/sys.stderr, but that hasn't been done for
# the dmypy client, so we just do the non-threadsafe thing.
def f(stdout: TextIO, stderr: TextIO) -> None:
old_stdout = sys.stdout
old_stderr = sys.stderr
try:
sys.stdout = stdout
sys.stderr = stderr
main(args)
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr

return _run(f)