LoginSignup
134

More than 5 years have passed since last update.

Pythonの主要なLint(pep8, pylint, flake8)の設定方法まとめ

Last updated at Posted at 2016-01-26

ちょっとしたメモ書き。

  • pep8
  • pylint
  • flake8

のそれぞれで、「一部の警告をチェック対象から除外する」などの設定を行いたいときの
設定ファイルの書き方と置き場所を整理。

参考:[Python]pep8とpylintの設定ファイルを作成して一部の警告を非表示にする - dackdive's blog
上記ブログ記事に flake8 の情報を追加したものです。

要約

いずれも、設定ファイルは ~/.config/ ディレクトリ下に作れば良い。(その他の選択肢もある)
ファイル名は

  • pep8 -> pep8
  • pylint -> pylintrc
  • flake8 -> flake8

と、pylint だけ注意。
設定ファイルの書式も pylint のみ異なる。

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 にあるように、

~/.config/pep8
[pep8]
ignore = E226,E302,E41
max-line-length = 160

ignore = の後ろにカンマ区切りで無視したい警告のコードを記述します。

コードはここから確認します。
http://pep8.readthedocs.org/en/latest/intro.html#error-codes

pylint

設定ファイルの場所

https://pylint.readthedocs.io/en/latest/user_guide/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 が存在するディレクトリの場合)、pylintrc を見つけるまで階層を上に辿る。
    これによりモジュール単位で pylintrc を設定することができる(し、ファイルがないモジュールはプロジェクトのルートにある pylintrc が使える)
  4. 環境変数 PYLINTRC で指定したファイル
  5. /root 以外の home ディレクトリがあった場合:
    1. home ディレクトリ直下の .pylintrc
    2. $HOME/.config/pylintrc
  6. /etc/pylintrc

といったところでしょうか。
working directory はおそらく pylint コマンドを実行した時の位置になるんだと思います。

私は pep8 と同じく ~/.config ディレクトリの下に配置するよう

~/.config/pylintrc

としました。

設定ファイルの書き方

--generate-rcfile オプションつきで pylint を実行すると設定ファイルのテンプレートが出力されるので、適当なファイルに保存します。

$ pylint --generate-rcfile > ~/.config/pylintrc

生成した pylintrc ファイルに disable= という変数があるのでそこに無視したい警告のコードを書きます。

~/.config/pylintrc
# 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#

また、手元で試した限りコード(または symbolic name)は改行で区切っても問題ないようです。

flake8

設定ファイルの場所

http://flake8.readthedocs.org/en/latest/config.html
によると Windows 以外は

~/.config/flake8

です。(Windows の場合 ~/.flake8)

また、プロジェクト単位では tox.ini または setup.cfg というファイルがあると
そちらに記載した設定も自動的に読み込まれるようです。
(書き方は↓と同じ)

設定ファイルの書き方

同じく http://flake8.readthedocs.org/en/latest/config.html によると、

~/.config/flake8
[flake8]
ignore = E226,E302,E41
max-line-length = 160
exclude = tests/*
max-complexity = 10

というように、pep8 と同じ形式で OK です。

コードはここから確認します。
F*** 系: http://flake8.readthedocs.org/en/latest/warnings.html
E***, W*** (pep8) 系: http://pep8.readthedocs.org/en/latest/intro.html#error-codes

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
134