-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow passing an output
to Run()
to capture the output of a run
#5295
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
Comments
Looking at this. |
Well, I've learned a lot about the reporters but it seems to work as expected. The passed-in I think the problem is in the proposed code, which has to seek to the beginning of the file object before reading from it. The below code seems to work as expected:
We could set the stream position to 0 when a file-like object is being used after all the checking but I'm not sure about the standard for if the user or module is responsible for this. If we don't want to handle it then this issue can be closed and I'll leave a reply on the StackOverflow question. |
Thank you for clearing that up @areveny (I think you just unstuck me on another issue regarding json report for 3.0). That is very counter intuitive, I think we should change the design so that it works like programmers expect it to work. What do you think ? If we change something the stackoverflow answer would still need to detail the behavior by pylint version. |
Personally, I think the example shown in the StackOverflow question and in the OP of this issue should work (perhaps with minor modification). The way the class and arguments are named creates the expectation that users should be able to do this. |
Thanks for the feedback. In preparing to implement this, I have a case for not making a change. Most notably
This makes me feel that if a user is working with lower level text streams which expose an interface for One could also imagine another program that relies on the current behavior of the cursor being left at the end of the file to continue writing additional text to the same stream. That would be a correct use case broken by this change. |
Look like the correct fix to do is to document the Or maybe (as we can't touch the stream), we can add a from io import StringIO
from pylint.reporters import text
from pylint.lint import Run
def main():
pylint_output = StringIO()
reporter = text.TextReporter(pylint_output)
Run(["test_file.py"], reporter=reporter, do_exit=False)
print(reporter) # Show messages emitted by this pylint run
if __name__ == '__main__':
main() |
I would like to update the documentation, most likely output.rst to describe some of the behaviors around reporters and injecting a custom reporter. To me, the stream interface and behavior we have now, used properly, is clean and well encapsulated. I think adding |
@areveny I think everybody would be very glad with updated documentation. That's often one of the things that's left for "soon". Since you clearly understand this particular part of codebase much better than I do I wanted to ask your opinion on one more solution: allow passing a keyword argument |
There are a couple of existing options for adding an external file output. But I do think there are some interesting ideas. Maybe I will come back to the reporter/output infrastructure using what I've learned to handle some of these different cases and output options. Possibly as part of the refactor of |
Just for the record, I ended up simply calling via the command line:
(For completeness, since I had to run this on legacy system with python2:)
If CLI is the intended interface, then just referring to it like this may be the easiest solution. The split between |
Thank you for clarifying @areveny, regarding the output file, would a context manager be a possibility ? with open("myfile.out", "w") as f:
reporter = text.TextReporter(f)
Run(["test_file.py"], reporter=reporter, do_exit=False)
# Something to do here ? |
That should be an already functional way of writing to an external file that is pretty similar to what happens when one of the external file command line options is set. TextReporter contains a text stream, so it leaves the cursor at the end when it's done, but we close the stream and read from the file so the stream position is irrelevant. But this setup would allow you to append to a longer stream, if you want to build a report of some sort, you could continue to write to the same |
Current problem
It seems logical to be able to capture the output of a
pylint
run with code that is similar to this:See for reference:
This StackOverflow question and #5260
Desired solution
A little investigation showed that the
output
argument passed to thereporter
doesn't actually get used as theoutput
of thePyLinter
class. By settingPyLinter._output
the expected behaviour might actually be triggerable, but normally we don't want users to directly access or initialise that class.As @Pierre-Sassoulas noted in #5260 (comment) it might be something to do with kwargs not being taken into account.
To achieve the expected behaviour we could either 1) make the
output
passed to thereporter
of theRun
instance be used as thePyLinter
output
or 2) allow passing a newoutput
argument toRun
which achieves the same thing.Additional context
No response
The text was updated successfully, but these errors were encountered: