3

I can't seem to get any return value when running pylint from Python code directly. Running it from the command line generates a nice report, with a summarized score at the bottom.

I've tried putting the return value of "Run" into a variable, and getting it's "reports" field - but it looks like some default template.

this is what I have:

from io import StringIO
from pylint.reporters import text
from pylint.lint import Run


def main():
    print("I will verify you build!")
    pylint_opts = ['--rcfile=pylintrc.txt', '--reports=n', 'utilities']
    pylint_output = StringIO()
    reporter = text.TextReporter(pylint_output)
    Run(pylint_opts, reporter=reporter, do_exit=False)
    print(pylint_output.read())


if __name__ == '__main__':
    main()

I'd expect some report here, but all I get is: " I will verify you build!

Process finished with exit code 0 "

Alexy Grabov
  • 151
  • 1
  • 13

2 Answers2

1

The code is alright but this is a bug in pylint that will be addressed by this issue.

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
1

I investigated the pylint issue. We ended up keeping current behavior because your code works with a slight adjustment:

def main():
    print("I will verify you build!")
    pylint_opts = ['--rcfile=pylintrc.txt', '--reports=n', 'utilities']
    pylint_output = StringIO()
    reporter = text.TextReporter(pylint_output)
    Run(pylint_opts, reporter=reporter, do_exit=False)
    print(pylint_output.getvalue())

StringIO is a stream that doesn't hide the stream position. Pylint writes to pylint_output correctly, but leaves the cursor at the end of the stream. pylint_output.read() only reads starting at the cursor.

You can call pylint_output.seek(0) to reset the stream position before calling pylint_output.read(). Alternatively, as demonstrated in the code block above, StringIO offers a method getvalue which will return the entire contents of the buffer regardless of cursor position.

The Pylint documentation will be updated with this example, so thanks for raising it.

areveny
  • 26
  • 2