17
17
18
18
Any pretty formatting is left to the caller.
19
19
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
+
20
26
Trivial example of code using this module:
21
27
22
28
import sys
33
39
print(result[1]) # stderr
34
40
35
41
print ('\n Exit status:', result[2])
42
+
36
43
"""
37
44
38
45
import sys
39
46
from io import StringIO
40
- from typing import List , Tuple
41
- from mypy .main import main
47
+ from typing import List , Tuple , Callable
42
48
43
49
44
- def run ( args : List [ str ]) -> Tuple [str , str , int ]:
50
+ def _run ( f : Callable [[], None ]) -> Tuple [str , str , int ]:
45
51
old_stdout = sys .stdout
46
52
new_stdout = StringIO ()
47
53
sys .stdout = new_stdout
@@ -51,7 +57,7 @@ def run(args: List[str]) -> Tuple[str, str, int]:
51
57
sys .stderr = new_stderr
52
58
53
59
try :
54
- main ( None , args = args )
60
+ f ( )
55
61
exit_status = 0
56
62
except SystemExit as system_exit :
57
63
exit_status = system_exit .code
@@ -60,3 +66,14 @@ def run(args: List[str]) -> Tuple[str, str, int]:
60
66
sys .stderr = old_stderr
61
67
62
68
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 ))
0 commit comments