LoginSignup
9
13

More than 5 years have passed since last update.

Pycharmの "pep8 indentation is not a multiple of four"や"Variable in function should be lowercase" という警告を抑制する方法

Last updated at Posted at 2015-08-13

はじめに

Pythonの開発はPycharmを使っています。

PythonのCoding styleの警告である

  • Variable in function should be lowercase
  • pep8 indentation is not a multiple of four (Indentサイズを半角スペース4つから2個に変更すると出る)

という警告が至る所で出てウザいので、これを抑制するPycharmでの設定方法をチラシの裏しておきます。

設定方法

  • Variable in function should be lowercaseを消す方法
    • FileSettingInspectionsを開き、PEP 8 coding naming convention viloationのチェックを外す
  • pep8 indentation is not a multiple of fourを消す方法
    • FileSettingInspectionsを開き、右ペインの検索画面で、PEP 8 coding style viloationを選択
    • Ignore errorsに、E111を追加して、Applyを実行

他の開発環境での同様の警告

  • その他の開発環境でPEP8のLinterを使う場合にも、特定のErrorをIgnoreする設定は大抵あるはずです。例えばATOMのPEP8 Linterの場合config.cson'ignoreErrorCodes': 'E111, E501'などと記載しておけば良いみたいです

PEP8について補足

PEP8について

主要なCoding Rule

  • Indentについて
    • Space 4つで1つのindent(Tab)にする事を推奨
    • このerrorを抑制したい場合は、E111, E114をignore
  • 関数名、変数名についての

    • 関数名、変数名:全て小文字で_で区切る、を推奨 (set_stream_loggerなど)
    • クラス名:Upper Camel Caseを推奨 (GetImageLocationなど)
    • Pycharm以外ではFlake8(PEP8のWrap library)pep8-naming(flake8のplugin)を入れて、N802/N806などをignoreする設定にすれば良いらしい(試した事無し)
  • 1行の長さ

    • 1行は79文字までを推奨
    • このerrorを抑制したい場合は、E501をignore
  • Import宣言について

    • 1行 1 libraryのImportを推奨
    • このerrorを抑制したい場合は、E401をignore
  • 演算子の前後の空白

    • =, ==, <, >, is, in, not inなどの演算子の前後には、1個スペースを入れる
    • このerrorを抑制したい場合は、E221, E222をignore

PEP8 Check Toolについて

pep8autopep8という2種類のToolがあります。

  • pep8について
    • 修正箇所を教えてくれるToolです
    • pip install pep8でInstallして、pep8 xxx.py --show-sourceとチェックしてほしい*.pyを指定すると、以下の様にError codeと該当箇所を吐いてくれます
    • 特定のErrorを無視する場合には、pep8 xxx.py --ignore=E111,E114,E501 --show-source
      E111/114はIndent Error, E501はLine Too Long
xxx.py:4:1: E302 expected 2 blank lines, found 1
def foo():
^
misc_test.py:5:9: E225 missing whitespace around operator
    msgs=['Hello','World!!']
        ^
misc_test.py:5:18: E231 missing whitespace after ','
    msgs=['Hello','World!!']
                 ^
misc_test.py:9:10: W292 no newline at end of file
    foo()
         ^
  • autopep8について
    • 自動で修正までしてくれるToolです
    • pip install autopep8でInstallして、autopep8 -i xxx.pyと自動修正してほしい*.pyを指定すると、問答無用にFileの中身を修正してくれます(ので実行時は注意です)
9
13
0

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
9
13