Skip to content

Commit abd319a

Browse files
authored
Provide an "API" for invoking the daemon (#5972)
Like the existing "API" for invoking mypy, it is just a thin wrapper around calling the main function.
1 parent f010360 commit abd319a

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

mypy/api.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
1818
Any pretty formatting is left to the caller.
1919
20+
The 'run_dmypy' function is similar, but instead mimics invocation of
21+
dmypy.
22+
23+
Note that these APIs don't support incremental generation of error
24+
messages.
25+
2026
Trivial example of code using this module:
2127
2228
import sys
@@ -33,15 +39,15 @@
3339
print(result[1]) # stderr
3440
3541
print ('\nExit status:', result[2])
42+
3643
"""
3744

3845
import sys
3946
from io import StringIO
40-
from typing import List, Tuple
41-
from mypy.main import main
47+
from typing import List, Tuple, Callable
4248

4349

44-
def run(args: List[str]) -> Tuple[str, str, int]:
50+
def _run(f: Callable[[], None]) -> Tuple[str, str, int]:
4551
old_stdout = sys.stdout
4652
new_stdout = StringIO()
4753
sys.stdout = new_stdout
@@ -51,7 +57,7 @@ def run(args: List[str]) -> Tuple[str, str, int]:
5157
sys.stderr = new_stderr
5258

5359
try:
54-
main(None, args=args)
60+
f()
5561
exit_status = 0
5662
except SystemExit as system_exit:
5763
exit_status = system_exit.code
@@ -60,3 +66,14 @@ def run(args: List[str]) -> Tuple[str, str, int]:
6066
sys.stderr = old_stderr
6167

6268
return new_stdout.getvalue(), new_stderr.getvalue(), exit_status
69+
70+
71+
def run(args: List[str]) -> Tuple[str, str, int]:
72+
# Lazy import to avoid needing to import all of mypy to call run_dmypy
73+
from mypy.main import main
74+
return _run(lambda: main(None, args=args))
75+
76+
77+
def run_dmypy(args: List[str]) -> Tuple[str, str, int]:
78+
from mypy.dmypy import main
79+
return _run(lambda: main(args))

mypy/dmypy.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import subprocess
1616
import sys
1717
import time
18+
import traceback
1819

19-
from typing import Any, Callable, Dict, Mapping, Optional, Tuple
20+
from typing import Any, Callable, Dict, Mapping, Optional, Tuple, List
2021

2122
from mypy.dmypy_util import STATUS_FILE, receive
2223
from mypy.ipc import IPCClient, IPCException
@@ -114,16 +115,21 @@ class BadStatus(Exception):
114115
pass
115116

116117

117-
def main() -> None:
118+
def main(argv: List[str]) -> None:
118119
"""The code is top-down."""
119-
args = parser.parse_args()
120+
args = parser.parse_args(argv)
120121
if not args.action:
121122
parser.print_usage()
122123
else:
123124
try:
124125
args.action(args)
125126
except BadStatus as err:
126127
sys.exit(err.args[0])
128+
except Exception:
129+
# We do this explicitly to avoid exceptions percolating up
130+
# through mypy.api invocations
131+
traceback.print_exc()
132+
sys.exit(2)
127133

128134

129135
ActionFunction = Callable[[argparse.Namespace], None]
@@ -483,4 +489,4 @@ def is_running() -> bool:
483489
# Run main().
484490

485491
if __name__ == '__main__':
486-
main()
492+
main(sys.argv[1:])

0 commit comments

Comments
 (0)