-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Conversation
@@ -0,0 +1,56 @@ | |||
''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: Use triple double quotes and the summary on the first line (example: """Single-line summary. ...
) for docstrings.
from mypy.main import main | ||
|
||
|
||
def run(params): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add type annotation.
sys.stdout = old_stdout | ||
sys.stderr = old_stderr | ||
|
||
return(new_stdout.getvalue(), new_stderr.getvalue()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nits: Add space after return
. No need to add parentheses around the return value expression.
This looks reasonable to me -- it's simple and not intrusive at all. I had just few minor nits. |
Will adapt the nits and resubmit |
This is very timely for me. I am looking forward to making use of it once it is merged. |
Hey @JdeH, are you still planning to push a new version responding to Jukka's code review? It sounded like it's almost there. |
Hi @gvanrossum , I've completed the changes two weeks ago, but the problem is I couldn't get the newest version of mypy properly installed to test it. I felt a bit stupid and tried to find out what to do, but after several hours I didn't make any progress. [EDIT] Found it, was installing mypy instead of mypy-lang :( |
Can test against newest version of mypy, since I can't get it installed. Since there seems be a need for this and it doesn't break anything, I'll commit anyhow. If there are problems with it let me know.
OK, I'll merge this now. Thanks! |
documentation and/or usage for this? |
|
After an earlier attempt (#2114), which was by @gvanrossum deemed to complex and restrictive in the current development stage of mypy, as promised here's another shot at it, indeed simpler and closer to the command line. This API uses mypy completely as a black box, not posing any demands to its internals.
Just
import mypy.api
and call therun
function, with exactly the params that would normally have been passed on the command line, and it will run mypy without starting an new interpreter instance or going through the OS.The
run
function will return a tuple of strings, the first one containing the output normally routed to std_out, the second one containing the output normally routed to std_err.While mypy primarily is meant as a command line tool, my usecase was that I wanted to integrate it into the Transcrypt Python to JS compiler without the overhead of going through the OS. I saw a few similar requests in the issues. Since this API doesn't have any interaction with the guts of mypy, it will cover these use cases without adding complexity or restrictions.
Since it follows the command line conventions closely, the command line docs and the comments in the source code provide enough info to cover the use of this API. Still, should any extra docs or usage examples be required, I'll happily provide that.
Kind regards
Jacques de Hooge