invoking pylint programmatically

后端 未结 7 875
难免孤独
难免孤独 2020-12-05 00:21

I\'d like to invoke the pylint checker, limited to the Error signalling part, as part of my unit testing. so I checked the pylint executable script, got to the pylint

7条回答
  •  生来不讨喜
    2020-12-05 00:55

    I got the same problem recently. syt is right, pylint.epylint got several methods in there. However they all call a subprocess in which python is launched again. In my case, this was getting quite slow.

    Building from mcarans answer, and finding that there is a flag exit, I did the following

    class WritableObject(object):
        "dummy output stream for pylint"
        def __init__(self):
            self.content = []
        def write(self, st):
            "dummy write"
            self.content.append(st)
        def read(self):
            "dummy read"
            return self.content
    def run_pylint(filename):
        "run pylint on the given file"
        from pylint import lint
        from pylint.reporters.text import TextReporter
        ARGS = ["-r","n", "--rcfile=rcpylint"]  # put your own here
        pylint_output = WritableObject()
        lint.Run([filename]+ARGS, reporter=TextReporter(pylint_output), exit=False)
        for l in pylint_output.read():
            do what ever you want with l...
    

    which is about 3 times faster in my case. With this I have been going through a whole project, using full output to check each source file, point errors, and rank all files from their note.

提交回复
热议问题