I have a Python script called my_code.py
. I am using:
import pylint
pylint.run_pylint(argv=["my_code.py"])
How to get all errors and store them in a list in the above snippet?
I have a Python script called my_code.py
. I am using:
import pylint
pylint.run_pylint(argv=["my_code.py"])
How to get all errors and store them in a list in the above snippet?
I adapted some code that found here:
from io import StringIO
from pylint.lint import Run
from pylint.reporters import text
def main():
pylint_opts = ["--reports=n", "toto.py"]
pylint_output = StringIO()
reporter = text.TextReporter(pylint_output)
Run(pylint_opts, reporter=reporter, do_exit=False)
a = pylint_output.getvalue()
print(a.split("\n"))
if __name__ == "__main__":
main()
Another option would be to write output to a file and then parse it.
Inspired by this answer, one way is to get the json
report and iterate through it.
import json
from io import StringIO
from pylint.lint import Run
from pylint.reporters import JSONReporter
reporter = StringIO()
Run(["my_code.py"], reporter=JSONReporter(reporter), do_exit=False)
results = json.loads(reporter.getvalue())
errors = {}
for i in results:
errors[i.get('message-id')] = i.get('message')
for k, v in errors.items():
print(k, v)
This will print each error code and the corresponding message.
For example, if you have my_code.py
as follows:
def foo(l: List[int]) -> List[int]:
return l[1:]
then after running my solution, you will get:
C0304 Final newline missing
C0114 Missing module docstring
C0116 Missing function or method docstring
C0104 Disallowed name "foo"
C0103 Argument name "l" doesn't conform to snake_case naming style
E0602 Undefined variable 'List'
When we run as normal like you did:
import pylint
pylint.run_pylint(argv=["knowde_com.py"])
output:
************* Module knowde_com
knowde_com.py:37:0: C0301: Line too long (105/100) (line-too-long)
knowde_com.py:43:0: C0301: Line too long (104/100) (line-too-long)
knowde_com.py:128:0: C0301: Line too long (117/100) (line-too-long)
knowde_com.py:157:0: C0301: Line too long (115/100) (line-too-long)
knowde_com.py:25:0: C0115: Missing class docstring (missing-class-docstring)
knowde_com.py:57:4: R0914: Too many local variables (23/15) (too-many-locals)
------------------------------------------------------------------
Your code has been rated at 8.24/10 (previous run: 8.24/10, +0.00)
To get it all into a list, Here's a simple solution:
import subprocess
command = "pylint knowde_com.py"
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, text=True)
result_list = result.stdout.split('\n')[1:-5]
print(result_list)
output:
['knowde_com.py:37:0: C0301: Line too long (105/100) (line-too-long)', 'knowde_com.py:43:0: C0301: Line too long (104/100) (line-too-long)', 'knowde_com.py:128:0: C0301: Line too long (117/100) (line-too-long)', 'knowde_com.py:157:0: C0301: Line too long (115/100) (line-too-long)', 'knowde_com.py:25:0: C0115: Missing class docstring (missing-class-docstring)', 'knowde_com.py:57:4: R0914: Too many local variables (23/15) (too-many-locals)']