プログラムで pylint を呼び出す
-
19-09-2019 - |
質問
単体テストの一環として、エラー通知部分に限定して 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
または他のソフトウェア:遠慮なく代替案を提案してください。ありがとう!
解決
を見てください。 pylint/epylint.py
を含む 二 プログラムで pylint を開始するさまざまな方法。
単純に次のように呼び出すこともできます。
from pylint.lint import Run
Run(['--errors-only', 'myfile.py'])
例えば。
他のヒント
最近同じ問題が発生しました。シットは正しい、 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 倍高速です。これにより、プロジェクト全体を実行し、完全な出力を使用して各ソース ファイルをチェックし、エラーを指摘し、メモからすべてのファイルをランク付けしました。
これに出会えてよかったです。ここでいくつかの答えといくつかのイニシアチブを使用して、次のことを考え出しました。
# 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"]
WritableObject クラスを作成する代わりに、StringIO を使用できます。StringIOにはwriteメソッドが含まれています。
import sys
try:
from io import StringIO
except:
from StringIO import StringIO
stdout = sys.stdout
sys.stdout = StringIO()
ARGS = ["-r","n", "--rcfile=rcpylint"]
r = lint.Run(['../test.py']+ARGS, exit=False)
test = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = stdout
print (test.split('\n'))
ソース:
pylint のもう 1 つのエントリ ポイントは、 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())
説明どおり ここ, 、pylint 自体が解析を実行し、期待される結果を stdout に正しく出力します。
注記:ある時点で pylint
インターフェイスを変更しました。上記の例は置き換える必要があります exit=False
と do_exit=False
. 。(@mad7777、@amit-tripathi)
(次のように学習しました) https://github.com/carsongee/pytest-pylint/issues/80)