pycodestyleのエラーの内容をある程度分かるように実際に出るエラーコードと共に意訳する。
コードは主にFlake8 Rulesから引用している。
タブと行末スペース関係はもしかすると勝手に変換/削除されているのでbadコードは無意味かもしれないので参考程度に。
E1 - インデントに関するエラー
E101 : Indentation contains mixed spaces and tabs
-
インデントにタブとスペースが混在
badclass Tab: def func(): passgoodclass Tab: def func(): pass
E111 : Indentation is not a multiple of four
-
インデントのスペースが4の倍数個でない
baddef func(): passgooddef func(): pass
E112 : Expected an indented block
-
インデントすべき箇所にインデントが存在しない
baddef func(): passgooddef func(): pass
E113 : Unexpected indentation
-
予期せぬインデントが存在する
baddef func(): passgooddef func(): pass
E114 : Indentation is not a multiple of four (comment)
-
インデントのスペースが4の倍数個でない(コメント)
baddef func(): # `pass` does nothing passgooddef func(): # `pass` does nothing pass
E115 : Expected an indented block (comment)
-
インデントすべき箇所にインデントが存在しない(コメント)
baddef func(): # `pass` does nothing passgooddef func(): # `pass` does nothing pass
E116 : Expected an indented block (comment)
-
予期せぬインデントが存在する(コメント)
bad# This is function def func(): passgood# This is function def func(): pass
E117 : over-indented
-
インデントが深すぎる
baddef func(): passgooddef func(): pass
E121 1, 2: Continuation line under-indented for hanging indent
-
継続行3におけるぶら下げインデントが浅い
bad{ 'key1': 'value', 'key2': 'value', }good{ 'key1': 'value', 'key2': 'value', }badprint("Python", ( "Rules"))goodprint("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', }badresult = function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )goodresult = function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )
E124 2 : Closing bracket does not match visual indentation
-
閉じ括弧のインデントが視覚的に統一できていない
badresult = function_that_takes_arguments('a', 'b', 'c', 'd', 'e', 'f', )goodresult = function_that_takes_arguments('a', 'b', 'c', 'd', 'e', 'f', )
E125 2 : Continuation line with same indent as next logical line
-
継続行3のインデントレベルが次に続く論理行と同じインデントレベルになっている
- 継続行が次に続く論理行と同じインデントになる場合は継続行のインデントレベルをさらに上げる
badif user is not None and user.is_admin or \ user.name == 'Grant': blah = 'yeahnah'goodif user is not None and user.is_admin or \ user.name == 'Grant': blah = 'yeahnah'
E126 1,2 : Continuation line over-indented for hanging indent
-
継続行3のインデントが深すぎる
bad{ 'key1': 'value', 'key2': 'value', }good{ 'key1': 'value', 'key2': 'value', }
E127 2 : Continuation line over-indented for visual indent
-
継続行3のインデントが、視覚的に統一するためのインデントよりも深い
badprint("Python", ("Hello", "World"))goodprint("Python", ("Hello", "World"))
E128 2 : Continuation line under-indented for visual indent
-
継続行3のインデントが、視覚的に統一するためのインデントよりも浅い
badprint("Python", ("Hello", "World"))goodprint("Python", ("Hello", "World"))
E129 2 : Visually indented line with same indent as next logical line
-
視覚的に統一するためのインデントが次に続く論理行と同じインデントレベルになっている
badif (row < 0 or module_count <= row or col < 0 or module_count <= col): raise Exception("%s,%s - %s" % (row, col, self.moduleCount))goodif (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オプションが使用されている場合にのみ発生)badmy_list = [ 1, 2, 3, 4, 5, 6, ]goodmy_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", }
E2 - 空白に関するエラー
E201 : Whitespace after '('
-
開き括弧の後に空白が入っている
badwith open( 'file.dat') as f: contents = f.read()goodwith open('file.dat') as f: contents = f.read()
E202 : Whitespace before ')'
-
閉じ括弧の前に空白が入っている
badwith open('file.dat' ) as f: contents = f.read()goodwith open('file.dat') as f: contents = f.read()
E203 : Whitespace before ':'
-
コロンの前に空白が入っている
badwith open('file.dat') as f : contents = f.read()goodwith open('file.dat') as f: contents = f.read()
E211 : Whitespace before '('
-
開き括弧の前に空白が入っている
badwith open ('file.dat') as f: contents = f.read()goodwith open('file.dat') as f: contents = f.read()
E221 : Multiple spaces before operator
-
演算子の前に複数の空白が存在する
badx = 10 y = x## 2goodx = 10 y = x * 2
E222 : Multiple spaces before operator
-
演算子の後に複数の空白が存在する
badx = 10 y = x * 2goodx = 10 y = x * 2
E223 : tab before operator
-
演算子の前にタブ文字が存在する
badx = 10 y = x * 2goodx = 10 y = x * 2
E224 : tab after operator
-
演算子の後にタブ文字が存在する
badx = 10 y = x * 2goodx = 10 y = x * 2
E225 : Missing whitespace around operator
-
演算子の前後に空白が存在しない
badif age>15: print('Can drive')goodif age > 15: print('Can drive')
E226 1 : Missing whitespace around arithmetic operator
-
算術演算子(
+,-,/,*)の前後に空白が存在しないbadage = 10+15goodage = 10 + 15
E227 : Missing whitespace around bitwise or shift operator
-
ビット演算子またはシフト演算子(
<<,>>,&,|,^)の前後に空白が存在しないbadremainder = 10%2goodremainder = 10 % 2
E228 : Missing whitespace around modulo operator
-
剰余演算子 (
%) の前後に空白が存在しないbadage = 10+15goodage = 10 + 15
E231 : Missing whitespace after ',', ';', or ':'
-
,,;,:の前後に空白が存在しないbadmy_tuple = 1,2,3goodmy_tuple = 1, 2, 3
E241 1 : Multiple spaces after ‘,’
-
,の後に複数の空白が存在するbadmy_tuple = 1, 2goodmy_tuple = 1, 2
E242 1 : Tab after ','
-
,の後にタブ文字が存在するbadmy_tuple = 1, 2, 3goodmy_tuple = 1, 2, 3
E251 : Unexpected spaces around keyword / parameter equals
-
関数定義時の
=前後に空白が存在するbaddef func(key1 = 'val1', key2 = 'val2'): return key1, key2gooddef func(key1='val1', key2='val2'): return key1, key2
E261 : At least two spaces before inline comment
-
インラインコメントの前には少なくとも2つのスペースが必要である
baddef print_name(self): print(self.name) # This comment needs an extra spacegooddef print_name(self): print(self.name) # Comment is correct now
E262 : Inline comment should start with '# '
-
インラインコメントは
#の後に空白が必要baddef print_name(self): print(self.name) #This comment needs a spacegooddef print_name(self): print(self.name) # Comment is correct now
E265 : Block comment should start with '# '
-
ブロックコメントは
#で始める必要がある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)
E266 : Too many leading '#' for block comment
-
ブロックコメントを開始する
#が多すぎるbad# Prints hello print('hello')good# Prints hello print('hello')
E271 : Multiple spaces after keyword
-
キーワードの後に複数の空白が存在する
baddef func(): passgooddef func(): pass
E272 : Multiple spaces before keyword
-
キーワードの前に複数の空白が存在する
baddef func(): if 1 in [1, 2, 3]: print('yep!')gooddef func(): if 1 in [1, 2, 3]: print('yep!')
E273 : Tab after keyword
-
キーワードの後にタブ文字が存在する
baddef func(): passgooddef func(): pass
E274 : Tab before keyword
-
キーワードの前にタブ文字が存在する
baddef func(): if 1 in [1, 2, 3]: print('yep!')gooddef func(): if 1 in [1, 2, 3]: print('yep!')
E275 : Missing whitespace after keyword
-
キーワードの後に空白が存在しない
badfrom collections import(namedtuple, defaultdict)goodfrom collections import (namedtuple, defaultdict)
E3 - 空行に関するエラー
E301 : Expected 1 blank line, found 0
-
クラスのメソッド間に1行の空白行が必要
badclass MyClass(object): def func1(): pass def func2(): passgoodclass MyClass(object): def func1(): pass def func2(): pass
E302 : Expected 2 blank lines, found 0
-
関数とクラスの間に2行の空白行が必要
baddef func1(): pass def func2(): passgooddef func1(): pass def func2(): pass
E303 : Too many blank lines (3)
-
空白行が多すぎる
baddef func1(): pass def func2(): passgooddef func1(): pass def func2(): pass
E304 : Blank lines found after function decorator
-
関数デコレータの後に空白行が存在する
badclass User(object): @property def name(self): passgoodclass 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()
```
E4 - Importに関するエラー
E401 : Multiple imports on one line
-
1行に複数回のインポートが行われている
badimport collections, os, sysgoodimport collections import os import sys
E402 : Module level import not at top of file
-
モジュールレベルのインポートがファイルの先頭以外で行われている
badimport locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') import sysgoodimport locale import sys locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
E5 - 行の長さに関するエラー
E501 2 : Line to long ((length) > 79 characters)
- 一行の文字数が多すぎる
- 推奨最大文字数は79文字
- 100または120文字に変更することは一般的
E502 : The backslash is redundant between brackets
-
括弧の中に冗長なバックスラッシュが存在している
badprint('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."')goodprint('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."')
E7 - ステートメントに関するエラー
E701 : Multiple statements on one line (colon)
-
一行に複数のステートメントが存在する(
:)badif x > 5: y = 10goodif x > 5: y = 10
E702 : Multiple statements on one line (semicolon)
-
一行に複数のステートメントが存在する(
;)badfrom gevent import monkey; monkey.patch_all()goodfrom gevent import monkey monkey.patch_all()
E703 : Statement ends with a semicolon
-
ステートメントがセミコロンで終了している
badprint('Hello world!');goodprint('Hello world!')
E704 1 : Multiple statements on one line (def)
-
関数定義の複数ステートメントが一行で行われている
baddef f(): passgooddef f(): pass
E711 2 : Comparison to none should be 'if cond is none:'
-
True, False, Noneといったシングルトンオブジェクトを同一性ではなく等価性で比較している
badif var != True: print("var is not equal to True") if var == None: print("var is equal to None")goodif 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との比較を等価演算子を用いて行っているbadx = True if x == True: print('True!')goodx = True if x is True: print('True!') # or simply: if x: print('True!')
E713 : Test for membership should be 'not in'
-
要素の検証に
inの結果の否定が使われているbadmy_list = [1, 2, 3] if not num in my_list: print(num)goodmy_list = [1, 2, 3] if num not in my_list: print(num)
E714 : Test for object identity should be 'is not'
-
オブジェクトの同一性の検証に
isの結果の否定が使われているbadif not user is None: print(user.name)goodif user is not None: print(user.name)
E721 2 : Do not compare types, use 'isinstance()'
-
型の比較に
isinstance()を使用していないbadif type(user) == User: print(user.name)goodif isinstance(user, User): print(user.name)
E722 : Do not use bare except, specify exception instead
-
例外捕捉時に例外クラスを指定していない
badtry: func() except: print("error")goodtry: func() except Exception: print("error")
E731 : Do not assign a lambda expression, use a def
-
ラムダ式を変数に代入している
badroot = lambda folder_name: os.path.join(BASE_DIR, folder_name)gooddef root(folder_name): return os.path.join(BASE_DIR, folder_name)
E741 : Do not use variables named 'l', 'O', or 'I'
-
l,O,Iといった紛らわしい変数名を使用しているbadl = []goodlist_name = []
E742 : Do not define classes named 'l', 'O', or 'I'
-
l,O,Iといった紛らわしいクラス名を使用しているbadclass I: def func(): passgoodclass Icon: def func(): pass
E743 : Do not define functions named 'l', 'O', or 'I'
-
l,O,Iといった紛らわしい関数名を使用しているbaddef O: passgooddef oh: pass
E9 - ランタイムエラー
E901 : SyntaxError or IndentationError
- 構文エラーまたはインデントのエラー
E902 : IOError
- 入出力エラー
W1 - インデントに関する警告
W191 : Indentation contains tabs
-
タブでインデントされている行が存在する
baddef func(): passgooddef func(): pass
W2 - 空白に関する警告
W291 : Trailing whitespace
-
行末に空白が存在する
baddef func(): passgooddef func(): pass
W292 : No newline at end of file
-
ファイルの末尾に改行が存在しない
baddef func(): passgooddef func(): pass
W293 : Blank line contains whitespace
-
空白行に空白またはタブが存在する
baddef first_func(): pass # This line contains four spaces def second_func(): passgooddef first_func(): pass def second_func(): pass
W3 - 空白行に関する警告
W391 : Blank line at end of file
-
ファイルの末尾に複数の空白行が存在する
baddef func(): passgooddef func(): pass
W5 - 改行に関する警告
W503 1 : Line break occurred before a binary operator
-
二項演算子の前に改行が存在する
- 現在非推奨,
W504と排他的な関係
badincome = (gross_wages + taxable_interest)goodincome = (gross_wages + taxable_interest) - 現在非推奨,
W504 1 : Line break occurred after a binary operator
-
二項演算子の後に改行が存在する
-
W503と排他的な関係
badincome = (gross_wages + taxable_interest)goodincome = (gross_wages + taxable_interest) -
W505 1 2 : Doc line too long ((length) > 79 characters)
-
コメントまたはdocstringの一行の文字数が多すぎる
- 79文字が基準だが、flake8では
max-doc-lengthの値を与えなければ警告されない
baddef func(): """ Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing """ passgooddef func(): """ Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing """ pass - 79文字が基準だが、flake8では
W6 - 廃止構文に関する警告
W601 : .has_key() is deprecated, use ‘in’
-
.has_key()メソッドの使用bad{'key': 'value'}.has_key('key')good'key' in {'key': 'value'}
W602 : Deprecated form of raising exception
-
非推奨の形式(
raise Exception, message)で例外を発生させているbaddef can_drive(age): if age < 16: raise ValueError, 'Not old enough to drive' return Truegooddef can_drive(age): if age < 16: raise ValueError('Not old enough to drive') return True
W603 : '<>' is deprecated, use '!='
-
非等価の比較に
<>を用いているbadassert 'test' <> 'testing'goodassert 'test' != 'testing'
W604 : Backticks are deprecated, use 'repr()'
-
Python3で廃止されたバッククォートによるオブジェクトの文字列化を試みている
badobj = MyObj() print(`obj`)goodobj = MyObj() print(repr(obj))
W605 : invalid escape sequence 'x'
-
無効なエスケープシーケンスを用いている
- バックスラッシュと値の組み合わせは全てエスケープシーケンスとみなされる
badregex = '\.png$'goodregex = r'\.png$'
W606 : ‘async’ and ‘await’ are reserved keywords starting with Python 3.7
-
Python 3.7以降の予約語である
asyncとawaitを用いているbaddef async(): passgooddef my_async(): pass
コメント
いいね以上の気持ちはコメントで