Based on few solutions here, I came with the following (mixing StringIO
to avoid creating a "simple class" when something already exists) and the TextReporter
from pylint:
from io import StringIO
from pylint.lint import Run
from pylint.reporters.text import TextReporter
files = ["my_file.py"]
pylint_options = ["--disable=line-too-long,import-error,fixme"]
pylint_output = StringIO()
Run(
files + pylint_options,
reporter=TextReporter(pylint_output),
exit=False,
)
for line in pylint_output.getvalue().splitlines():
... # do something with each line, like filtering / extracting
You can also use ParseableTextReporter
instead of TextReporter
and the results will be slightly different (maybe easier to parse):
With TextReporter
:
************* Module my_file
my_file.py:65:0: W0613: Unused argument 'args' (unused-argument)
my_file.py:65:0: W0613: Unused argument 'kwargs' (unused-argument)
my_file.py:90:4: C0116: Missing function or method docstring (missing-function-docstring)
my_file.py:93:4: C0116: Missing function or method docstring (missing-function-docstring)
my_file.py:96:4: C0116: Missing function or method docstring (missing-function-docstring)
my_file.py:99:4: C0116: Missing function or method docstring (missing-function-docstring)
my_file.py:102:4: C0116: Missing function or method docstring (missing-function-docstring)
my_file.py:183:0: C0116: Missing function or method docstring (missing-function-docstring)
------------------------------------------------------------------
Your code has been rated at 9.61/10 (previous run: 9.61/10, +0.00)
With ParseableTextReporter
:
************* Module my_file
my_file.py:65: [W0613(unused-argument), CoverageSummaryTable.__init__] Unused argument 'args'
my_file.py:65: [W0613(unused-argument), CoverageSummaryTable.__init__] Unused argument 'kwargs'
my_file.py:90: [C0116(missing-function-docstring), ProtoLogger.debug] Missing function or method docstring
my_file.py:93: [C0116(missing-function-docstring), ProtoLogger.info] Missing function or method docstring
my_file.py:96: [C0116(missing-function-docstring), ProtoLogger.warning] Missing function or method docstring
my_file.py:99: [C0116(missing-function-docstring), ProtoLogger.error] Missing function or method docstring
my_file.py:102: [C0116(missing-function-docstring), ProtoLogger.critical] Missing function or method docstring
my_file.py:183: [C0116(missing-function-docstring), coverage_output] Missing function or method docstring
------------------------------------------------------------------
Your code has been rated at 9.61/10 (previous run: 9.61/10, +0.00)
It's up to you :)
Thanks to @mad7777, @Amit Tripathi, and @mcarans