動作環境
python3
PyCharm(無料版)
PEP8に準拠したコーディングをしたい場合の設定
PyCharm左上のメニュー欄から
File -> Settings -> Editor -> Inspections -> Python -> PEP8 coding style violation から week warning を warning に上げる。
また、その下の PEP8 naming convention violation の設定も warningに変更する。
エラーの表示文と直し方
以下、具体的なエラーの対応方法です。
これらのエラーは、静的解析といい、PyCharmでは無料版においても即時にチェックしてくれます。この結果、バグの防止や可読性が上がります。
PEP8 do not use bare except
と警告が出たら
except:
↓
except FileNotFoundError:
というように、エラーのパターンを記載する。
一度エラーを出して確認すれば、何のエラーかわかります。
Function name should be lowercase
ReadFile
↓
read_file
Remove redundant parentheses
余計なカッコを削除すればよい
class ClassA():
↓
class ClassA:
PEP8: expected 2 blank lines, found 1
1行の空白を2行に変更すればよい
PEP8: expected 2 black lines after class or function definition, found 1
1行の空白を2行に変更すればよい
PEP8: block comment should start with '# '
#のあとに半角のスペースを入れる
PEP8: identation contains mixed spaces and tabs
インデンテーションでスペースとタブが混在している
Method "xxx" may be 'static'
メソッドの一行前に@staticmethodを付ける
Shadows built-in name 'xx'
既存の(組込みの)名前 'xx' とかぶっている(shadow 覆い隠す)。
バグの温床になるので無くすようにする
continuation line under-indented for visual indent
(視覚的なインデントのために継続行がインデントされていません)
引数を最初の行におかずに二行目から列挙していく。ちょっと複雑
simplify chained comparison
(連鎖している比較を簡素化しなさい)
PyCharmの場合、Alt + Shift + Enterを押すと、自動で修正してくれる