作为单元测试的一部分,我想调用限制在错误信号部分的pylint检查器。所以我检查了pylint可执行脚本,进入了pylint.lint.Run
助手类,在那里我迷失了很长的__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
或其他软件:随意提出替代方案。谢谢!
答案 0 :(得分:23)
查看包含两个的pylint/epylint.py
以不同的方式以编程方式启动pylint。
您也可以直接致电:
from pylint.lint import Run
Run(['--errors-only', 'myfile.py'])
例如。
答案 1 :(得分:16)
我最近遇到了同样的问题。
syt是对的,pylint.epylint
有几种方法。然而,它们都调用了再次启动python的子进程。就我而言,这变得非常缓慢。
从mcarans回答构建,并发现有一个标志出口,我做了以下
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倍。 有了这个,我一直在经历一个完整的项目,使用完整的输出来检查每个源文件,指出错误,并从他们的笔记中排列所有文件。
答案 2 :(得分:3)
我很高兴我遇到过这个。我在这里使用了一些答案,并提出了一些主动性:
# 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)
上面的Args是一个字符串列表,例如。 [“-r”,“n”,“myfile.py”]
答案 3 :(得分:3)
我们可以使用StringIO而不是创建WritableObject类。 StringIO包含写方法。
db/development.sqlite3
来源:
答案 4 :(得分:1)
pylint的另一个入口点是epylint.py_run
函数,它实现了stdout和stderr拦截。但是,如下面的代码所示,pylint似乎没有在stdout中编写报告:
from pylint import epylint
pylint_stdout, pylint_stderr = epylint.py_run(__file__, return_std=True)
print(pylint_stdout.getvalue()) # -> there is just the final rank, no report nor message
print(pylint_stderr.getvalue())
现在,我发现来自API的pylint和来自CLI的pylint不使用相同的默认参数。因此,您只需提供pylint所需的参数。
from pylint import epylint
options = '--enable=all' # all messages will be shown
options += '--reports=y' # also print the reports (ascii tables at the end)
pylint_stdout, pylint_stderr = epylint.py_run(__file__ + ' ' + options, return_std=True)
print(pylint_stdout.getvalue())
print(pylint_stderr.getvalue())
正如here所述,pylint将执行解析本身,并将在stdout中正确输出预期结果。
答案 5 :(得分:0)
注意:
在pylint
某个时刻更改了界面。以上示例需要将exit=False
替换为do_exit=False
。 (@ mad7777,@ amit-tripathi)
答案 6 :(得分:0)
这里是一个包装器,我用它来编程方式调用pylint,所以我有一个--fail-under arg来覆盖默认的pylint退出代码(对于CI而言为usefull)。此代码段已使用pylint 2.3.1进行了测试
""" Execute pylint and fail if score not reached. """
import argparse
import sys
from pylint import lint
desc = "PyLint wrapper that add the --fail-under option."\
" All other arguments are passed to pylint."
parser = argparse.ArgumentParser(description=desc, allow_abbrev=False)
parser.add_argument('--fail-under', dest='threshold', type=float, default=8,
help='If the final score is more than THRESHOLD, exit with'
' exitcode 0, and pylint\'s exitcode otherwise.')
args, remaining_args = parser.parse_known_args()
threshold = args.threshold
run = lint.Run(remaining_args, do_exit=False)
score = run.linter.stats['global_note']
if score < threshold:
sys.exit(run.linter.msg_status)