hackingとflake8プラグイン
2013-08-01 - ⧖ 3.0 minhacking はOpenStackプロジェクトで使用されているPythonのコーディングスタイルをチェックするツールです。 Google Python Style Guide を規約のベースに使用しており、 flake8 プラグインの仕組みを使って実装されています。
同じような目的に使用されるツールとしては、 pep8 や pyflakes 、 pylint などがあります。
使い方
インストール
$ pip install hacking
$ flake8 --version
2.0 (pep8: 1.4.5, pyflakes: 0.7.2, mccabe: 0.2.1, GitCheckCommitTitleBug: 0.0.1, GitCheckCommitTitleLength: 0.0.1, GitCheckCommitTitlePeriodEnding: 0.0.1, hacking.core: 0.0.1, ProxyChecker: 0.0.1)
## not install hacking
2.0 (pep8: 1.4.6, pyflakes: 0.7.3, mccabe: 0.2.1)
ツール起動
$ flake8 autopep8.py
autopep8.py:51:5: H302 import only modules.'from StringIO import StringIO' does not import a module
autopep8.py:53:5: H302 import only modules.'from io import StringIO' does not import a module
autopep8.py:56:1: H302 import only modules.'from optparse import OptionParser' does not import a module
autopep8.py:56:1: H306 imports not in alphabetical order (tokenize, optparse.optionparser)
autopep8.py:57:1: H306 imports not in alphabetical order (optparse.optionparser, difflib)
autopep8.py:233:19: H101 Use TODO(NAME )
autopep8.py:1681:5: H302 import only modules.'from lib2to3.refactor import RefactoringTool' does not import a module
flake8拡張の仕組み
ドキュメント と 他のflake8拡張の実装を見たほうが理解が速いと思うので、 詳しい解説はそちらに任せるとして、 ざっくり説明するとプラグインの処理を関数(またはクラス)として準備し、 setup.pyのentry_pointsに 'flake8.extension' を指定すればflake8拡張として組み込めます。 Hxxx のようなIDは4桁以内で定義する必要があります。
例として文字列 'Hello World' を検出するflake8拡張を作成してみます。
# flake8_helloworld.py
import pep8
def check_helloworld (physical_line ):
if pep8 .noqa (physical_line ):
return
pos = physical_line .find ('Hello World' )
if - 1 != pos :
return pos , 'HW01 use "Hello World"'
check_helloworld .name = name = 'flake8-helloworld'
check_helloworld .version = __version__
# setup.py
from setuptools import setup
setup (
name = 'flake8-helloworld' ,
version = '0.1' ,
:
entry_points = {
'flake8.extension' : [
'HW01 = flake8_helloworld:check_helloworld' ,
],
},
:
install_requires = ['flake8>=2.0' ],
)
上記の "Hello World" を検知する拡張は GitHubにあげておきました 。
ちなみに hacking で追加しているチェック項目は以下です。
[entry_points ]
flake8 .extension =
H000 = hacking .core :ProxyChecks
H101 = hacking .core :hacking_todo_format
H102 = hacking .core :hacking_has_license
H103 = hacking .core :hacking_has_correct_license
H201 = hacking .core :hacking_except_format
H202 = hacking .core :hacking_except_format_assert
H231 = hacking .core :hacking_python3x_except_compatible
H232 = hacking .core :hacking_python3x_octal_literals
H233 = hacking .core :hacking_python3x_print_function
H301 = hacking .core :hacking_import_rules
H306 = hacking .core :hacking_import_alphabetical
H401 = hacking .core :hacking_docstring_start_space
H402 = hacking .core :hacking_docstring_one_line
H403 = hacking .core :hacking_docstring_multiline_end
H404 = hacking .core :hacking_docstring_multiline_start
H501 = hacking .core :hacking_no_locals
H601 = hacking .core :hacking_no_cr
H700 = hacking .core :hacking_localization_strings
H801 = hacking .core :OnceGitCheckCommitTitleBug
H802 = hacking .core :OnceGitCheckCommitTitleLength
H803 = hacking .core :OnceGitCheckCommitTitlePeriodEnding
H901 = hacking .core :hacking_is_not
H902 = hacking .core :hacking_not_in
H8xx などはgitのコミットメッセージのチェックまで行っているようです。
最後に
hacking を採用するかどうかは別として、 flake8拡張を使ったチェック手法は色々と参考になることが多かったです。
OpenStackプロジェクトで pbr などのPythonで作られたツールを使用しているようなので、 また記事にできそうなら紹介してみます。
では。