flake8エラーコード - yoshitaku55/ai_knowledge GitHub Wiki
PythonPEP8lintflake8pycodestyle
pycodestyleのエラーの内容をある程度分かるように実際に出るエラーコードと共に意訳する。 コードは主にFlake8 Rulesから引用している。 タブと行末スペース関係はもしかすると勝手に変換/削除されているのでbadコードは無意味かもしれないので参考程度に。
-
インデントにタブとスペースが混在
bad
class Tab: def func(): pass
good
class Tab: def func(): pass
-
インデントのスペースが4の倍数個でない
bad
def func(): pass
good
def func(): pass
-
インデントすべき箇所にインデントが存在しない
bad
def func(): pass
good
def func(): pass
-
予期せぬインデントが存在する
bad
def func(): pass
good
def func(): pass
-
インデントのスペースが4の倍数個でない(コメント)
bad
def func(): # `pass` does nothing pass
good
def func(): # `pass` does nothing pass
-
インデントすべき箇所にインデントが存在しない(コメント)
bad
def func(): # `pass` does nothing pass
good
def func(): # `pass` does nothing pass
-
予期せぬインデントが存在する(コメント)
bad
# This is function def func(): pass
good
# This is function def func(): pass
-
インデントが深すぎる
bad
def func(): pass
good
def func(): pass
-
継続行3におけるぶら下げインデントが浅い
bad
{ 'key1': 'value', 'key2': 'value', }
good
{ 'key1': 'value', 'key2': 'value', }
bad
print("Python", ( "Rules"))
good
print("Python", ( "Rules"))
E122
2 : Continuation line missing indentation or outdented
-
継続行3のインデントが存在しないか、インデントレベルが上回っている
bad
{ 'key1': 'value', 'key2': 'value', }
good
{ 'key1': 'value', 'key2': 'value', }
E123
1 : Closing bracket does not match indentation of opening bracket’s line
-
閉じ括弧が開き括弧の行のインデントレベルと一致していない
bad
{ 'key1': 'value', 'key2': 'value', }
good
{ 'key1': 'value', 'key2': 'value', }
bad
result = function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )
good
result = function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )
E124
2 : Closing bracket does not match visual indentation
-
閉じ括弧のインデントが視覚的に統一できていない
bad
result = function_that_takes_arguments('a', 'b', 'c', 'd', 'e', 'f', )
good
result = function_that_takes_arguments('a', 'b', 'c', 'd', 'e', 'f', )
E125
2 : Continuation line with same indent as next logical line
-
継続行3のインデントレベルが次に続く論理行と同じインデントレベルになっている
- 継続行が次に続く論理行と同じインデントになる場合は継続行のインデントレベルをさらに上げる
bad
if user is not None and user.is_admin or \ user.name == 'Grant': blah = 'yeahnah'
good
if user is not None and user.is_admin or \ user.name == 'Grant': blah = 'yeahnah'
-
継続行3のインデントが深すぎる
bad
{ 'key1': 'value', 'key2': 'value', }
good
{ 'key1': 'value', 'key2': 'value', }
E127
2 : Continuation line over-indented for visual indent
-
継続行3のインデントが、視覚的に統一するためのインデントよりも深い
bad
print("Python", ("Hello", "World"))
good
print("Python", ("Hello", "World"))
E128
2 : Continuation line under-indented for visual indent
-
継続行3のインデントが、視覚的に統一するためのインデントよりも浅い
bad
print("Python", ("Hello", "World"))
good
print("Python", ("Hello", "World"))
E129
2 : Visually indented line with same indent as next logical line
-
視覚的に統一するためのインデントが次に続く論理行と同じインデントレベルになっている
bad
if (row < 0 or module_count <= row or col < 0 or module_count <= col): raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
good
if (row < 0 or module_count <= row or col < 0 or module_count <= col): raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
E131
2 : Continuation line unaligned for hanging indent
-
継続行3のぶら下げのインデントの位置が合っていない
bad
{ "key1": "value", "key2": "value value value" "value", }
good
{ "key1": "value", "key2": "value value value" "value", }
E133
1 : Closing bracket is missing indentation
-
閉じ括弧にインデントがない
(
--hang-closing
オプションが使用されている場合にのみ発生)bad
my_list = [ 1, 2, 3, 4, 5, 6, ]
good
my_list = [ 1, 2, 3, 4, 5, 6, ]
E131
2 : Continuation line unaligned for hanging indent
-
継続行3のぶら下げのインデントの位置が合っていない
bad
{ "key1": "value", "key2": "value value value" "value", }
good
{ "key1": "value", "key2": "value value value" "value", }
-
開き括弧の後に空白が入っている
bad
with open( 'file.dat') as f: contents = f.read()
good
with open('file.dat') as f: contents = f.read()
-
閉じ括弧の前に空白が入っている
bad
with open('file.dat' ) as f: contents = f.read()
good
with open('file.dat') as f: contents = f.read()
-
コロンの前に空白が入っている
bad
with open('file.dat') as f : contents = f.read()
good
with open('file.dat') as f: contents = f.read()
-
開き括弧の前に空白が入っている
bad
with open ('file.dat') as f: contents = f.read()
good
with open('file.dat') as f: contents = f.read()
-
演算子の前に複数の空白が存在する
bad
x = 10 y = x## 2
good
x = 10 y = x * 2
-
演算子の後に複数の空白が存在する
bad
x = 10 y = x * 2
good
x = 10 y = x * 2
-
演算子の前にタブ文字が存在する
bad
x = 10 y = x * 2
good
x = 10 y = x * 2
-
演算子の後にタブ文字が存在する
bad
x = 10 y = x * 2
good
x = 10 y = x * 2
-
演算子の前後に空白が存在しない
bad
if age>15: print('Can drive')
good
if age > 15: print('Can drive')
E226
1 : Missing whitespace around arithmetic operator
-
算術演算子(
+
,-
,/
,*
)の前後に空白が存在しないbad
age = 10+15
good
age = 10 + 15
-
ビット演算子またはシフト演算子(
<<
,>>
,&
,|
,^
)の前後に空白が存在しないbad
remainder = 10%2
good
remainder = 10 % 2
-
剰余演算子 (
%
) の前後に空白が存在しないbad
age = 10+15
good
age = 10 + 15
-
,
,;
,:
の前後に空白が存在しないbad
my_tuple = 1,2,3
good
my_tuple = 1, 2, 3
E241
1 : Multiple spaces after ‘,’
-
,
の後に複数の空白が存在するbad
my_tuple = 1, 2
good
my_tuple = 1, 2
E242
1 : Tab after ','
-
,
の後にタブ文字が存在するbad
my_tuple = 1, 2, 3
good
my_tuple = 1, 2, 3
-
関数定義時の
=
前後に空白が存在するbad
def func(key1 = 'val1', key2 = 'val2'): return key1, key2
good
def func(key1='val1', key2='val2'): return key1, key2
-
インラインコメントの前には少なくとも2つのスペースが必要である
bad
def print_name(self): print(self.name) # This comment needs an extra space
good
def print_name(self): print(self.name) # Comment is correct now
-
インラインコメントは
#
の後に空白が必要bad
def print_name(self): print(self.name) #This comment needs a space
good
def print_name(self): print(self.name) # Comment is correct now
-
ブロックコメントは
#
で始める必要があるbad
#This comment needs a space def print_name(self): print(self.name)
good
# Comment is correct now def print_name(self): print(self.name)
-
ブロックコメントを開始する
#
が多すぎるbad
# Prints hello print('hello')
good
# Prints hello print('hello')
-
キーワードの後に複数の空白が存在する
bad
def func(): pass
good
def func(): pass
-
キーワードの前に複数の空白が存在する
bad
def func(): if 1 in [1, 2, 3]: print('yep!')
good
def func(): if 1 in [1, 2, 3]: print('yep!')
-
キーワードの後にタブ文字が存在する
bad
def func(): pass
good
def func(): pass
-
キーワードの前にタブ文字が存在する
bad
def func(): if 1 in [1, 2, 3]: print('yep!')
good
def func(): if 1 in [1, 2, 3]: print('yep!')
-
キーワードの後に空白が存在しない
bad
from collections import(namedtuple, defaultdict)
good
from collections import (namedtuple, defaultdict)
-
クラスのメソッド間に1行の空白行が必要
bad
class MyClass(object): def func1(): pass def func2(): pass
good
class MyClass(object): def func1(): pass def func2(): pass
-
関数とクラスの間に2行の空白行が必要
bad
def func1(): pass def func2(): pass
good
def func1(): pass def func2(): pass
-
空白行が多すぎる
bad
def func1(): pass def func2(): pass
good
def func1(): pass def func2(): pass
-
関数デコレータの後に空白行が存在する
bad
class User(object): @property def name(self): pass
good
class User(object): @property def name(self): pass
## E305
: Expected 2 blank lines after end of function or class
* 関数またはクラスの終了後は2行の空白行が必要
```python:bad
class User(object):
pass
user = User()
```
```python:good
class User(object):
pass
user = User()
```
-
1行に複数回のインポートが行われている
bad
import collections, os, sys
good
import collections import os import sys
-
モジュールレベルのインポートがファイルの先頭以外で行われている
bad
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') import sys
good
import locale import sys locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
E501
2 : Line to long ((length) > 79 characters)
- 一行の文字数が多すぎる
- 推奨最大文字数は79文字
- 100または120文字に変更することは一般的
-
括弧の中に冗長なバックスラッシュが存在している
bad
print('Four score and seven years ago our fathers brought '\ 'forth, upon this continent, a new nation, conceived '\ 'in liberty, and dedicated to the proposition that '\ '"all men are created equal."')
good
print('Four score and seven years ago our fathers brought ' 'forth, upon this continent, a new nation, conceived ' 'in liberty, and dedicated to the proposition that ' '"all men are created equal."')
-
一行に複数のステートメントが存在する(
:
)bad
if x > 5: y = 10
good
if x > 5: y = 10
-
一行に複数のステートメントが存在する(
;
)bad
from gevent import monkey; monkey.patch_all()
good
from gevent import monkey monkey.patch_all()
-
ステートメントがセミコロンで終了している
bad
print('Hello world!');
good
print('Hello world!')
E704
1 : Multiple statements on one line (def)
-
関数定義の複数ステートメントが一行で行われている
bad
def f(): pass
good
def f(): pass
E711
2 : Comparison to none should be 'if cond is none:'
-
True, False, Noneといったシングルトンオブジェクトを同一性ではなく等価性で比較している
bad
if var != True: print("var is not equal to True") if var == None: print("var is equal to None")
good
if var is not True print("var is not True") if var is None print("var is None")
E712
2 : Comparison to true should be 'if cond is true:' or 'if cond:'
-
True
との比較を等価演算子を用いて行っているbad
x = True if x == True: print('True!')
good
x = True if x is True: print('True!') # or simply: if x: print('True!')
-
要素の検証に
in
の結果の否定が使われているbad
my_list = [1, 2, 3] if not num in my_list: print(num)
good
my_list = [1, 2, 3] if num not in my_list: print(num)
-
オブジェクトの同一性の検証に
is
の結果の否定が使われているbad
if not user is None: print(user.name)
good
if user is not None: print(user.name)
E721
2 : Do not compare types, use 'isinstance()'
-
型の比較に
isinstance()
を使用していないbad
if type(user) == User: print(user.name)
good
if isinstance(user, User): print(user.name)
-
例外捕捉時に例外クラスを指定していない
bad
try: func() except: print("error")
good
try: func() except Exception: print("error")
-
ラムダ式を変数に代入している
bad
root = lambda folder_name: os.path.join(BASE_DIR, folder_name)
good
def root(folder_name): return os.path.join(BASE_DIR, folder_name)
-
l
,O
,I
といった紛らわしい変数名を使用しているbad
l = []
good
list_name = []
-
l
,O
,I
といった紛らわしいクラス名を使用しているbad
class I: def func(): pass
good
class Icon: def func(): pass
-
l
,O
,I
といった紛らわしい関数名を使用しているbad
def O: pass
good
def oh: pass
- 構文エラーまたはインデントのエラー
- 入出力エラー
-
タブでインデントされている行が存在する
bad
def func(): pass
good
def func(): pass
-
行末に空白が存在する
bad
def func(): pass
good
def func(): pass
-
ファイルの末尾に改行が存在しない
bad
def func(): pass
good
def func(): pass
-
空白行に空白またはタブが存在する
bad
def first_func(): pass # This line contains four spaces def second_func(): pass
good
def first_func(): pass def second_func(): pass
-
ファイルの末尾に複数の空白行が存在する
bad
def func(): pass
good
def func(): pass
W503
1 : Line break occurred before a binary operator
-
二項演算子の前に改行が存在する
- 現在非推奨,
W504
と排他的な関係
bad
income = (gross_wages + taxable_interest)
good
income = (gross_wages + taxable_interest)
- 現在非推奨,
W504
1 : Line break occurred after a binary operator
-
二項演算子の後に改行が存在する
-
W503
と排他的な関係
bad
income = (gross_wages + taxable_interest)
good
income = (gross_wages + taxable_interest)
-
-
コメントまたはdocstringの一行の文字数が多すぎる
- 79文字が基準だが、flake8では
max-doc-length
の値を与えなければ警告されない
bad
def func(): """ Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing """ pass
good
def func(): """ Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing """ pass
- 79文字が基準だが、flake8では
-
.has_key()
メソッドの使用bad
{'key': 'value'}.has_key('key')
good
'key' in {'key': 'value'}
-
非推奨の形式(
raise Exception, message
)で例外を発生させているbad
def can_drive(age): if age < 16: raise ValueError, 'Not old enough to drive' return True
good
def can_drive(age): if age < 16: raise ValueError('Not old enough to drive') return True
-
非等価の比較に
<>
を用いているbad
assert 'test' <> 'testing'
good
assert 'test' != 'testing'
-
Python3で廃止されたバッククォートによるオブジェクトの文字列化を試みている
bad
obj = MyObj() print(`obj`)
good
obj = MyObj() print(repr(obj))
-
無効なエスケープシーケンスを用いている
- バックスラッシュと値の組み合わせは全てエスケープシーケンスとみなされる
bad
regex = '\.png$'
good
regex = r'\.png$'
-
Python 3.7以降の予約語である
async
とawait
を用いているbad
def async(): pass
good
def my_async(): pass