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

Conversation

JdeH
Copy link
Contributor

@JdeH JdeH commented Nov 11, 2016

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 the run 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

@@ -0,0 +1,56 @@
'''
Copy link
Collaborator

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):
Copy link
Collaborator

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())
Copy link
Collaborator

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.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 25, 2016

This looks reasonable to me -- it's simple and not intrusive at all. I had just few minor nits.

@JdeH
Copy link
Contributor Author

JdeH commented Nov 25, 2016

Will adapt the nits and resubmit

@wtpayne
Copy link

wtpayne commented Dec 13, 2016

This is very timely for me. I am looking forward to making use of it once it is merged.

@gvanrossum
Copy link
Member

Hey @JdeH, are you still planning to push a new version responding to Jukka's code review? It sounded like it's almost there.

@JdeH
Copy link
Contributor Author

JdeH commented Dec 14, 2016

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 :(
api.py seems to work alright now on a simple example.

JdeH added 2 commits December 14, 2016 09:52
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.
@gvanrossum
Copy link
Member

OK, I'll merge this now. Thanks!

@gvanrossum gvanrossum merged commit 0fe8670 into python:master Dec 14, 2016
@ohadraz
Copy link

ohadraz commented Jul 27, 2020

documentation and/or usage for this?

@dandavison
Copy link

documentation and/or usage for this?

https://mypy.readthedocs.io/en/stable/extending_mypy.html#integrating-mypy-into-another-python-application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants