2

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?

Bsh
  • 345
  • 1
  • 9
  • 1
    Isn't this literally covered by the docs over on https://pylint.readthedocs.io/en/latest/development_guide/api/pylint.html? But really: run it as a command with all the flags you need to get the output you need, and then capture its stdout output. It's _meant_ to be used as command line utility, not really "in a script" (even if you _technically_ can). – Mike 'Pomax' Kamermans Jul 25 '23 at 15:14

3 Answers3

1

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.

matleg
  • 618
  • 4
  • 11
1

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'
Coder
  • 431
  • 4
  • 11
0

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)']
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24