pep8 や pylint はインストールしたままの状態だと必要以上に警告が表示されてうっとおしいので、
無視したい警告などを設定ファイルに記載します。その方法をメモ。
※pylint については以前 Syntastic の設定方法の時にも触れました
[vim]Syntasticでpython用の設定をする - dackdive's blog
(前提)
PC は Mac OS X Yosemite (v10.10.5)、pep8 と pylint のバージョンはそれぞれ 1.7.0、1.5.4 で確認。
(2016/01/26追記)
flake8 の設定も追加して Qiita に投稿しました。
pep8
設定ファイルの置き場
http://pep8.readthedocs.org/en/latest/intro.html#configuration
によると、
If on Windows:
~\.pep8
Otherwise, if theXDG_CONFIG_HOMEenvironment variable is defined:
XDG_CONFIG_HOME/pep8
Else ifXDG_CONFIG_HOMEis not defined:
~/.config/pep8
なので、XDG_CONFIG_HOME を設定していない Mac の場合は ~/.config/pep8 で良さそうです。
設定ファイルの書き方
同じく http://pep8.readthedocs.org/en/latest/intro.html#configuration にあるように、
[pep8] ignore = E226,E302,E41 max-line-length = 160
ignore = の後ろにカンマ区切りで無視したい警告のコードを記述します。
コードはここから確認します。
http://pep8.readthedocs.org/en/latest/intro.html#error-codes
pylint
設定ファイルの置き場
http://docs.pylint.org/run.html#command-line-options
によると、pylint の設定ファイルは以下のような順序で探索して最初に見つかったものが読み込まれるそう。
pylintrcin the current working directory.pylintrcin the current working directory- If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a
pylintrcfile. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an__init__.pyfile.- The file named by environment variable
PYLINTRC- if you have a home directory which isn’t
/root:
.pylintrcin your home directory.config/pylintrcin your home directory/etc/pylintrc
ざっくり和訳すると
- 現在の working directory にある
pylintrc - 現在の working directory にある
.pylintrc - 現在の working directory が Python のモジュールのディレクトリだった場合(
__init__.pyが存在するディレクトリの場合)、pythonrcを見つけるまで階層を上に辿る。
これによりモジュール単位でpylintrcを設定することができる(し、ファイルがないモジュールはプロジェクトのルートにあるpythonrcが使える) - 環境変数
PYLINTRCで指定したファイル /root以外の home ディレクトリがあった場合:- home ディレクトリ直下の
.pylintrc $HOME/.config/pylintrc
- home ディレクトリ直下の
/etc/pylintrc
といったところでしょうか。
working directory はおそらく pylint コマンドを実行した時の位置になるんだと思います。
私は pep8 と同じく ~/.config ディレクトリの下にしました。
設定ファイルの書き方
--generate-rcfile オプションつきで pylint を実行すると設定ファイルのテンプレートが出力されるので、適当なファイルに保存します。
$ pylint --generate-rcfile > ~/.config/pylintrc
出力する警告の設定方法
先ほどのコマンドで生成した pylintrc ファイルに disable= という変数があるのでそこに無視したい警告のコードを書きます。
# Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this # option multiple times (only on the command line, not in the configuration # file where it should appear only once).You can also use "--disable=all" to # disable everything first and then reenable specific checks. For example, if # you want to run only the similarities checker, you can use "--disable=all # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" disable= oct-method, ext-method-called, C0111
コードは表示されるメッセージを元にここから検索できます。
http://pylint-messages.wikidot.com/all-codes
また、コードのかわりに symbolic name と呼ばれる、コードよりも内容が推測できる名前での指定もできるようです。
(上の oct-method や ext-method-called がそうです)
http://docs.pylint.org/faq.html#do-i-have-to-remember-all-these-numbers
symbolic name はここからコードを元に調べられそう。
http://docs.pylint.org/features.html#
また、手元で試した限り改行を挟んでも認識してくれるようです。