Skip to content

Provide an "API" for invoking the daemon #5972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
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
25 changes: 21 additions & 4 deletions mypy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

Any pretty formatting is left to the caller.

The 'run_dmypy' function is similar, but instead mimics invocation of
dmypy.

Note that these APIs don't support incremental generation of error
messages.

Trivial example of code using this module:

import sys
Expand All @@ -33,15 +39,15 @@
print(result[1]) # stderr

print ('\nExit status:', result[2])

"""

import sys
from io import StringIO
from typing import List, Tuple
from mypy.main import main
from typing import List, Tuple, Callable


def run(args: List[str]) -> Tuple[str, str, int]:
def _run(f: Callable[[], None]) -> Tuple[str, str, int]:
old_stdout = sys.stdout
new_stdout = StringIO()
sys.stdout = new_stdout
Expand All @@ -51,7 +57,7 @@ def run(args: List[str]) -> Tuple[str, str, int]:
sys.stderr = new_stderr

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

return new_stdout.getvalue(), new_stderr.getvalue(), exit_status


def run(args: List[str]) -> Tuple[str, str, int]:
# Lazy import to avoid needing to import all of mypy to call run_dmypy
from mypy.main import main
return _run(lambda: main(None, args=args))


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this doesn't support incremental flushing of errors (maybe we will do this at some point), but it is the same for normal runs, so probably OK. Maybe add a comment about this.

14 changes: 10 additions & 4 deletions mypy/dmypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import subprocess
import sys
import time
import traceback

from typing import Any, Callable, Dict, Mapping, Optional, Tuple
from typing import Any, Callable, Dict, Mapping, Optional, Tuple, List

from mypy.dmypy_util import STATUS_FILE, receive
from mypy.ipc import IPCClient, IPCException
Expand Down Expand Up @@ -114,16 +115,21 @@ class BadStatus(Exception):
pass


def main() -> None:
def main(argv: List[str]) -> None:
"""The code is top-down."""
args = parser.parse_args()
args = parser.parse_args(argv)
if not args.action:
parser.print_usage()
else:
try:
args.action(args)
except BadStatus as err:
sys.exit(err.args[0])
except Exception:
# We do this explicitly to avoid exceptions percolating up
# through mypy.api invocations
traceback.print_exc()
sys.exit(2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this choice, I think there are still some situations where 1 can be returned on a traceback in the server, but I will fix those separately later.



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

if __name__ == '__main__':
main()
main(sys.argv[1:])