以编程方式调用pylint

2024-04-25 22:02:36 发布

您现在位置:Python中文网/ 问答频道 /正文

作为单元测试的一部分,我想调用pylint检查器(仅限于错误信号部分)。所以我检查了pylint可执行脚本,找到了pylint.lint.Runhelper类,在那里我迷失在一个很长的__init__函数中,最后调用了sys.exit()

有人试过并设法做到了吗?

梦想计划是:

if __name__ == '__main__':
  import pylint.lint
  pylint.lint.something(__file__, justerrors=True)
  # now continue with unit testing

有什么线索吗?除了“复制__init__方法并跳过sys.exit()”,我是说?

我不需要由pylint运行测试,也可以是pyflakes或其他软件:请随意提出替代方案。谢谢!


Tags: 函数脚本if信号init错误sysexit
3条回答

我最近也遇到同样的问题。 syt是对的,pylint.epylint有几种方法。但是,它们都调用一个子进程,在该子进程中,python将再次启动。就我而言,这是相当缓慢的。

从mcarans answer构建,发现有一个标志出口,我做了以下操作

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...

在我的情况下大约快3倍。 有了这个我已经经历了一个完整的项目,使用完整的输出检查每个源文件,点错误,并从他们的笔记排列所有文件。

看看pylint/epylint.py,它包含两种不同的启动pylint的方法。

您也可以简单地调用:

from pylint.lint import Run
Run(['--errors-only', 'myfile.py']) 

例如。

我很高兴我遇到了这个。我用了这里的一些答案和一些倡议来提出:

# a simple class with a write method
class WritableObject:
    def __init__(self):
        self.content = []
    def write(self, string):
        self.content.append(string)
pylint_output = WritableObject()

pylint = lint.Run(args, reporter=ParseableTextReporter(pylint_output), exit=False)

上面的参数是一个字符串列表,例如。[“-r”,“n”,“myfile.py”]

相关问题 更多 >

    热门问题