Skip to content

Non-intrusive API for calling mypy directly from another python application. #2439

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 24 commits into from
Dec 14, 2016
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
983d357
first commit
JdeH Sep 6, 2016
31158a4
indentation repaired, tabs will be spaces in the end...
JdeH Sep 6, 2016
fd4c36d
api, first working version
JdeH Sep 9, 2016
18b5c93
Merge remote-tracking branch 'upstream/master'
JdeH Sep 9, 2016
7834fd4
param name for ErrorInfo ctor changed to 'message'
JdeH Sep 9, 2016
103cb55
param type of 'line' for 'ErrorInfo' ctor changed to 'int'
JdeH Sep 9, 2016
6a67bbd
Formatting made acceptable to lint
JdeH Sep 9, 2016
9dcb42d
Formatting made acceptable to lint (2)
JdeH Sep 9, 2016
d43401b
Formatting made acceptable to lint (3)
JdeH Sep 9, 2016
38a5bd7
Formatting made acceptable to lint (4)
JdeH Sep 9, 2016
a4fcd04
Formatting made acceptable to lint (5)
JdeH Sep 9, 2016
c9f2a97
Formatting made acceptable to lint (6)
JdeH Sep 9, 2016
124a5a4
Formatting made acceptable to lint (7)
JdeH Sep 9, 2016
9b2dab4
Merge remote-tracking branch 'upstream/master'
JdeH Nov 11, 2016
0158d37
Non intrusive API module added. Allows calling mypy from Python
JdeH Nov 11, 2016
b4420eb
test code removed
JdeH Nov 11, 2016
bab1cbb
Empty doc removed
JdeH Nov 11, 2016
e2dcd7e
Non intrusive API module added. Allows calling mypy from Python
JdeH Nov 11, 2016
56e663c
Output for std_out and std_err returned separately
JdeH Nov 11, 2016
78bcb82
Adapted to satisfy lint
JdeH Nov 11, 2016
326a41d
Adapted to satisfy lint 2
JdeH Nov 11, 2016
8b8efba
Unneeded traceback import removed
JdeH Nov 12, 2016
efde03d
Nits resolved.
JdeH Dec 14, 2016
1325363
Removed trailing whitespace, tested simple example
JdeH Dec 14, 2016
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
56 changes: 56 additions & 0 deletions mypy/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""This module makes it possible to use mypy as part of a Python application.

Since mypy still changes, the API was kept utterly simple and non-intrusive.
It just mimics command line activation without starting a new interpreter.
So the normal docs about the mypy command line apply.
Changes in the command line version of mypy will be immediately useable.

Just import this module and then call the 'run' function with exactly the
string you would have passed to mypy from the command line.
Function 'run' returns a tuple of strings: (<normal_report>, <error_report>),
in which <normal_report> is what mypy normally writes to sys.stdout and
<error_report> is what mypy normally writes to sys.stderr.
Any pretty formatting is left to the caller.

Trivial example of code using this module:

import sys
from mypy import api

result = api.run(' '.join(sys.argv[1:]))

if result[0]:
print('\nType checking report:\n')
print(result[0]) # stdout

if result[1]:
print('\nError report:\n')
print(result[1]) # stderr
"""

import sys
from io import StringIO
from typing import Tuple
from mypy.main import main


def run(params: str) -> Tuple[str, str]:
sys.argv = [''] + params.split()

old_stdout = sys.stdout
new_stdout = StringIO()
sys.stdout = new_stdout

old_stderr = sys.stderr
new_stderr = StringIO()
sys.stderr = new_stderr

try:
main(None)
except:
pass

sys.stdout = old_stdout
sys.stderr = old_stderr

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