【日記】Pythonの静的解析ツールflake8を使ってみた
昨日の記事の続きです。
まずflake8のインストールから。
pip install flake8
コマンドラインで次のように実行。
flake8 ソースファイル.py
または
flake8
手持ちのファイルを1つ解析した結果がこちら。
lambda_function.py:1:1: F401 'os' imported but unused
lambda_function.py:10:1: F401 'chrome.DictBunch' imported but unused
lambda_function.py:10:26: E231 missing whitespace after ','
lambda_function.py:12:1: F401 'selenium.webdriver' imported but unused
lambda_function.py:20:1: E302 expected 2 blank lines, found 1
lambda_function.py:42:1: W293 blank line contains whitespace
lambda_function.py:45:1: W293 blank line contains whitespace
lambda_function.py:46:57: E231 missing whitespace after ','
lambda_function.py:46:80: E501 line too long (97 > 79 characters)
lambda_function.py:47:80: E501 line too long (96 > 79 characters)
lambda_function.py:50:1: W293 blank line contains whitespace
lambda_function.py:51:34: E231 missing whitespace after ':'
lambda_function.py:51:47: E231 missing whitespace after ':'
lambda_function.py:52:1: W293 blank line contains whitespace
lambda_function.py:53:9: E265 block comment should start with '# '
lambda_function.py:54:1: W293 blank line contains whitespace
lambda_function.py:59:1: W293 blank line contains whitespace
lambda_function.py:61:18: E221 multiple spaces before operator
lambda_function.py:62:1: W293 blank line contains whitespace
lambda_function.py:70:1: W293 blank line contains whitespace
lambda_function.py:74:1: E302 expected 2 blank lines, found 1
lambda_function.py:75:11: E231 missing whitespace after ':'
lambda_function.py:76:11: E231 missing whitespace after ':'
lambda_function.py:79:1: W293 blank line contains whitespace
lambda_function.py:96:80: E501 line too long (93 > 79 characters)
lambda_function.py:98:1: W293 blank line contains whitespace
lambda_function.py:112:80: E501 line too long (82 > 79 characters)
lambda_function.py:117:9: F841 local variable 'buttons' is assigned to but never used
lambda_function.py:118:1: W293 blank line contains whitespace
lambda_function.py:131:80: E501 line too long (80 > 79 characters)
lambda_function.py:135:13: E265 block comment should start with '# '
lambda_function.py:135:80: E501 line too long (83 > 79 characters)
lambda_function.py:136:13: E265 block comment should start with '# '
lambda_function.py:136:80: E501 line too long (84 > 79 characters)
lambda_function.py:137:13: E265 block comment should start with '# '
lambda_function.py:153:13: E265 block comment should start with '# '
lambda_function.py:154:80: E501 line too long (83 > 79 characters)
lambda_function.py:155:80: E501 line too long (101 > 79 characters)
lambda_function.py:155:97: E231 missing whitespace after ','
lambda_function.py:156:9: E722 do not use bare 'except'
lambda_function.py:161:1: E302 expected 2 blank lines, found 1
lambda_function.py:162:11: E231 missing whitespace after ':'
lambda_function.py:163:11: E231 missing whitespace after ':'
lambda_function.py:166:1: W293 blank line contains whitespace
lambda_function.py:171:80: E501 line too long (106 > 79 characters)
lambda_function.py:179:1: W293 blank line contains whitespace
lambda_function.py:182:80: E501 line too long (82 > 79 characters)
lambda_function.py:184:1: W293 blank line contains whitespace
lambda_function.py:189:1: W293 blank line contains whitespace
lambda_function.py:191:5: E303 too many blank lines (2)
lambda_function.py:202:80: E501 line too long (94 > 79 characters)
lambda_function.py:211:9: F841 local variable 'css_selector_infodialog' is assigned to but never used
lambda_function.py:211:80: E501 line too long (96 > 79 characters)
lambda_function.py:212:1: W293 blank line contains whitespace
lambda_function.py:214:80: E501 line too long (82 > 79 characters)
lambda_function.py:216:1: W293 blank line contains whitespace
lambda_function.py:218:80: E501 line too long (113 > 79 characters)
lambda_function.py:220:13: E722 do not use bare 'except'
lambda_function.py:224:80: E501 line too long (111 > 79 characters)
lambda_function.py:226:13: E722 do not use bare 'except'
lambda_function.py:229:9: E722 do not use bare 'except'
lambda_function.py:235:80: E501 line too long (93 > 79 characters)
lambda_function.py:236:80: E501 line too long (81 > 79 characters)
lambda_function.py:238:80: E501 line too long (115 > 79 characters)
lambda_function.py:238:111: E231 missing whitespace after ','
lambda_function.py:239:9: E722 do not use bare 'except'
lambda_function.py:244:1: E302 expected 2 blank lines, found 1
lambda_function.py:245:11: E231 missing whitespace after ':'
lambda_function.py:246:11: E231 missing whitespace after ':'
lambda_function.py:249:1: W293 blank line contains whitespace
lambda_function.py:259:1: W293 blank line contains whitespace
lambda_function.py:280:1: W293 blank line contains whitespace
lambda_function.py:282:1: W293 blank line contains whitespace
lambda_function.py:291:10: W291 trailing whitespace
lambda_function.py:295:16: F821 undefined name 'NoSuchElementException'
lambda_function.py:302:1: W293 blank line contains whitespace
lambda_function.py:305:16: F821 undefined name 'NoSuchElementException'
lambda_function.py:307:1: W293 blank line contains whitespace
lambda_function.py:318:46: W605 invalid escape sequence '\d'
lambda_function.py:318:80: E501 line too long (96 > 79 characters)
lambda_function.py:319:9: E722 do not use bare 'except'
lambda_function.py:324:1: E302 expected 2 blank lines, found 1
lambda_function.py:325:11: E231 missing whitespace after ':'
lambda_function.py:326:11: E231 missing whitespace after ':'
lambda_function.py:329:1: W293 blank line contains whitespace
lambda_function.py:348:1: W293 blank line contains whitespace
lambda_function.py:353:1: W293 blank line contains whitespace
lambda_function.py:361:1: W293 blank line contains whitespace
lambda_function.py:368:80: E501 line too long (102 > 79 characters)
lambda_function.py:374:1: W293 blank line contains whitespace
lambda_function.py:379:80: E501 line too long (161 > 79 characters)
lambda_function.py:380:80: E501 line too long (125 > 79 characters)
lambda_function.py:380:103: E231 missing whitespace after ','
lambda_function.py:381:9: E722 do not use bare 'except'
lambda_function.py:394:1: E302 expected 2 blank lines, found 1
lambda_function.py:396:15: W291 trailing whitespace
lambda_function.py:400:1: W293 blank line contains whitespace
lambda_function.py:422:1: W293 blank line contains whitespace
lambda_function.py:424:1: W293 blank line contains whitespace
lambda_function.py:430:1: W293 blank line contains whitespace
lambda_function.py:431:14: E211 whitespace before '('
lambda_function.py:432:1: W293 blank line contains whitespace
lambda_function.py:435:1: W293 blank line contains whitespace
lambda_function.py:438:1: W293 blank line contains whitespace
lambda_function.py:443:1: W293 blank line contains whitespace
lambda_function.py:448:1: W293 blank line contains whitespace
lambda_function.py:463:1: W293 blank line contains whitespace
ボロボロですね。
E211 whitespace before '('
関数呼び出しなどの"("の前に空白がある
E221 multiple spaces before operator
演算子の前に複数の空白が存在する
E231 missing whitespace after ','
E231 missing whitespace after ':'
カンマ、コロン、セミコロンの前後に空白が存在しない(上記のエラーは後ろに存在しない場合)
E265 block comment should start with '# '
ブロックコメントは”# "で始めるべき(#のあとにスペースを入れる)。
E302 expected 2 blank lines, found 1
関数とクラス定義の間に2行の空白行が必要(上記のエラーは1行しか空白行がない)。
E303 too many blank lines (2)
空白行が多すぎる場合
E501 line too long (xxx > 79 characters)
1行が長過ぎる。79文字以下にするべき。
E722 do not use bare 'except'
例外捕捉時に例外クラスを指定していない。
try:
# 何かしらのコード
except: # ←ここで例外クラスを指定するべき
pass
F401 'モジュール名' imported but unused
インポートしたけど使っていないモジュール
F821 undefined name 'NoSuchElementException'
未定義の名前。たまたまそのロジックを通ってないから問題が起きていないだけですね。
F841 local variable '変数名' is assigned to but never used
ローカル変数を定義したけど使っていない。
W291 trailing whitespace
行末に空白がある。
W293 blank line contains whitespace
空白行にスペースが含まれている。
W605 invalid escape sequence '\d'
無効なエスケープシーケンスを用いている。
正規表現を使う場合は、raw文字列を使わないとこの警告がでます。