LoginSignup
6

More than 5 years have passed since last update.

pylintのwarning解消メモ

Last updated at Posted at 2018-01-14

はじめに

pylintを何気なく入れてみたらゃwarningがたくさん出たので,内容をまとめておきます。

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

とあります。どこに書いても設定は反映されますが,今回はプロジェクトのルートに.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行で終わる場合は、同じ行を """ で閉じるようにしてください。
    クラスの最初とメソッドの最初にも必要になります。

とありますので,モジュール,関数,クラス,メソッドには"""で始まり,"""で終わるコメントを書くようにしましょう。

参考

pylint 出力結果の警告についての備忘録
Python のコーディング規約 PEP8 に準拠する
pep8-ja|

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
6