-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix sys.stdout overriding in mypy.api (#6125) #6129
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
7975e09 to
00a803c
Compare
Overriding sys.stdout and sys.stderr in mypy.api is not threadsafe. This causes problems sometimes when using the api in pyls for example.
00a803c to
c465807
Compare
msullivan
left a comment
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.
Thanks for this! I left some comments, and additionally:
There are some things this misses:
- There are a handful of error message
prints inbuild.py - In
errors.py, there are a bunch of prints inreport_internal_errorfor displaying a traceback. (I think that a case /could/ be made for having those bypass the API capture, though we'd probably also want to arrange for the failure to bypass that as well? By using aMypyInternalErrorexception that we turn intosys.exit(2)in__main__.pybut let propagate in the api?) - The
argparse.ArgumentParserargument processing will print to sys.stderr. To avoid this I think we need to create a subclass that overrides theerrormethod. - Possibly others?
This is all pretty doable, but it makes me nervous about missing things.
It makes me wonder if the stable API ought to just use subprocess and invoke mypy in a fresh interpreter? It would be slower, but easier to make correct.
(Of course, I added mypy.api.run_dmypy specifically to avoid the performance penalty associated with interpreter startup, but we could make that an unstable API and document its thread safety limitations.)
| def run_dmypy(args: List[str]) -> Tuple[str, str, int]: | ||
| from mypy.dmypy import main | ||
| return _run(lambda: main(args)) | ||
| return _run(lambda stdout, stderr: main(args)) |
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.
We actually do need to worry about mypy.dmypy, since it does have this API (which has a Dropbox internal client).
If you don't want to fix mypy.dmypy now, it is fine to leave run_dmypy API thread-safety as a TODO, but in that case we need to continue hijacking sys.stdout and sys.stderr in run_dmypy.
|
|
||
| def main(script_path: Optional[str], args: Optional[List[str]] = None) -> None: | ||
| def main(script_path: Optional[str], args: Optional[List[str]] = None, | ||
| stdout: TextIO = sys.stdout, |
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.
We shouldn't use sys.stdout as a default argument value, because it is evaluated at module load time, not when the function is called. This means that if something else hijacks stdout, we will ignore that and write to the original one.
In cases where it needs to have a default value, have it be an Optional[TextIO] defaulting to None, and do something along the lines of stdout = stdout or sys.stdout. I think a bunch of functions can just be changed to have it not have a default value.
|
|
||
| def process_options(args: List[str], | ||
| stdout: TextIO = sys.stdout, | ||
| stderr: TextIO = sys.stderr, |
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.
I'd drop the stdout argument to this function and all the functions it call that don't need it.
Thanks I'll check that and try and look around to see if we missed anything else.
Propagating exceptions through the api sounds like a wise decision. For example in def fail(msg: str, stderr: TextIO) -> None:
stderr.write('%s\n' % msg)
sys.exit(1)Would it be better to raise an exception instead of
Ah yes this little guy https://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.error needs to change.
This is actually what we were first planning to do in https://github.com/tomv564/pyls-mypy/blob/master/pyls_mypy/plugin.py#L56, with something like: res = subprocess.run(
["python", "-m", "mypy", *args],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
report = res.stdout
errors = res.stderr
This could also be a solution, I like it! Have the normal Let me first work with your comments and we can go from there. |
|
If you’re just going to use subprocess, why bother with mypy.api at all? |
|
Should this be closed in light of #6750 being merged? |
|
Superseded by #6750. |
Overriding sys.stdout and sys.stderr in mypy.api is not threadsafe.
This causes problems sometimes when using the api in pyls for example.
I am struggling a little with getting the right type for
f, was expecting something likeUnion[Callable[[TextIO, TextIO], None], Callable[[], None]]to work.