はじめに
pylintを何気なく入れてみたらゃwarningがたくさん出たので,内容をまとめておきます。
pylintで設定を変更する方法
pylintrc
in the current working directory
-
.pylintrc
in 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
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. - The file named by environment variable
PYLINTRC
- if you have a home directory which isn’t
/root
:-
.pylintrc
in your home directory -
.config/pylintrc
in your home directory
-
/etc/pylintrc
とあります。どこに書いても設定は反映されますが,今回はプロジェクトのルートに.pylintrc
を追加しました。
pylintのテンプレの追加は以下でできます。
$ pylint --generate-rcfile > .pylintrc
Warning
Warningの内容は以下でした。超基本的。
インデントの文字数
Bad indentation. Found 2 spaces, expected 4 (bad-indentation)
Bad indentation. Found 4 spaces, expected 8 (bad-indentation)
インデントの文字数が設定と違うので怒られていました。
自分の環境ではスペース2文字分のインデントとなっているのが原因。これはpylintの設定を変更して対応しました。
PEP8(Pythonコードのコーディング規約)によるとこの対応はよろしくないことがわかりました。PEP8に,
1レベルインデントするごとに、スペースを4つ使いましょう。
とあるので,Pythonを各ときは4スペース使うようにした方がよさそうです。
末尾の空白
Trailing whitespace (trailing-whitespace)
末尾に不要な空白がないためにでてしまっているよう。
PEP8によると,
余計な空白文字を使うのはやめましょう
ということのようです。
末尾や,ブラケットのはじめと終わりなどにスペース入れるのはやめたほうがよいですね。
ファイル,クラス,メソッドの説明がない
Missing module docstring (missing-docstring)
Missing class docstring (missing-docstring)
Missing method docstring (missing-docstring)
モジュール(ファイル)の最初にこのモジュールの説明をコメントしておかないとでるようです。コメント書きましょう。
PEP8において,
すべての公開されているモジュールや関数、クラス、メソッドの docstring を書いてください。docstring は公開されていないメソッドには不要ですが、そのメソッドが何をしているのかは説明すべきです。このコメントは def の行のあとに置くべきです。
PEP 257 は良い docstring の規約です。もっとも重要なのは、複数行の docstring は """ だけからなる行で閉じることです。
docstring が1行で終わる場合は、同じ行を """ で閉じるようにしてください。
クラスの最初とメソッドの最初にも必要になります。
とありますので,モジュール,関数,クラス,メソッドには"""
で始まり,"""
で終わるコメントを書くようにしましょう。