dackdive's blog

新米webエンジニアによる技術ブログ。JavaScript(React), Salesforce, Python など

[Python]pep8とpylintの設定ファイルを作成して一部の警告を非表示にする

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 the XDG_CONFIG_HOME environment variable is defined:
 XDG_CONFIG_HOME/pep8
Else if XDG_CONFIG_HOME is 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 の設定ファイルは以下のような順序で探索して最初に見つかったものが読み込まれるそう。

  1. pylintrc in the current working directory
  2. .pylintrc in the current working directory
  3. If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. 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__.py file.
  4. The file named by environment variable PYLINTRC
  5. if you have a home directory which isn’t /root:
    1. .pylintrc in your home directory
    2. .config/pylintrc in your home directory
  6. /etc/pylintrc

ざっくり和訳すると

  1. 現在の working directory にある pylintrc
  2. 現在の working directory にある .pylintrc
  3. 現在の working directory が Python のモジュールのディレクトリだった場合(__init__.py が存在するディレクトリの場合)、pythonrc を見つけるまで階層を上に辿る。
    これによりモジュール単位で pylintrc を設定することができる(し、ファイルがないモジュールはプロジェクトのルートにある pythonrc が使える)
  4. 環境変数 PYLINTRC で指定したファイル
  5. /root 以外の home ディレクトリがあった場合:
    1. home ディレクトリ直下の .pylintrc
    2. $HOME/.config/pylintrc
  6. /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-methodext-method-called がそうです)
http://docs.pylint.org/faq.html#do-i-have-to-remember-all-these-numbers

symbolic name はここからコードを元に調べられそう。
http://docs.pylint.org/features.html#

また、手元で試した限り改行を挟んでも認識してくれるようです。