flake8を試す
flake8 2.0 : Python Package Index
flake8はpythonのコードチェッカーで、以下の3つのツールのラッパーとなっている
- pyflakes
- pep8
- Ned Batchelder's McCabe script
McCabe scriptというのは循環的複雑度をチェックするもの(らしい)
mccabe 0.2.1 : Python Package Index
循環的複雑度 - Wikipedia
インストール
$ pip install flake8
ヘルプをみてみる
$ flake8 --help Usage: flake8 [options] input ... Options: --version show program's version number and exit -h, --help show this help message and exit -v, --verbose print status messages, or debug with -vv -q, --quiet report only file names, or nothing with -qq --first show first occurrence of each error --exclude=patterns exclude files or directories which match these comma separated patterns (default: .svn,CVS,.bzr,.hg,.git,__pycache__) --filename=patterns when parsing directories, only check filenames matching these comma separated patterns (default: *.py) --select=errors select errors and warnings (e.g. E, --ignore=errors skip errors and warnings (e.g. E4,W) --show-source show source code for each error --show-pep8 show text of PEP 8 for each error (implies --first) --statistics count errors and warnings --count print total number of errors and warnings to standard error and set exit code to 1 if total is not null --max-line-length=n set maximum allowed line length (default: 79) --hang-closing hang closing bracket instead of matching indentation of opening bracket's line --format=format set the error format [default|pylint|<custom>] --diff report only lines changed according to the unified diff received on STDIN --exit-zero exit with code 0 even if there are errors --builtins=BUILTINS define more built-ins, comma separated --max-complexity=MAX_COMPLEXITY McCabe complexity threshold --install-hook Install the appropriate hook for this repository. Testing Options: --benchmark measure processing speed Configuration: The project options are read from the [flake8] section of the tox.ini file or the setup.cfg file located in any parent folder of the path(s) being processed. Allowed options are: exclude, filename, select, ignore, max-line-length, hang-closing, count, format, quiet, show- pep8, show-source, statistics, verbose, builtins, max-complexity. --config=path user config file location (default: /Users/yuhei/.config/flake8)
手元にあったdjangoプロジェクトに対して実行してみる
flake8 . --filename="*.py" --exclude=migrations --statistics ./app/models.py:1:1: F401 'models' imported but unused ./decision_table/settings.py:7:80: E501 line too long (82 > 79 characters) ./decision_table/settings.py:79:1: E122 continuation line missing indentation or outdented ./decision_table/settings.py:89:1: E122 continuation line missing indentation or outdented ./decision_table/urls.py:9:5: E128 continuation line under-indented for visual indent ./decision_table/urls.py:26:55: W292 no newline at end of file ./decision_table/settings/__init__.py:1:1: W391 blank line at end of file 2 E122 continuation line missing indentation or outdented 1 E128 continuation line under-indented for visual indent 1 E501 line too long (82 > 79 characters) 1 F401 'models' imported but unused 1 W292 no newline at end of file 1 W391 blank line at end of file
southを使っている場合はmigrations以下のファイルがE501に引っかかるのでexcludeにしたほうがいいと思う
statisticオプションを指定すると、それぞれのエラーに対する件数が最後に表示される
djangoに対して実行してみる
1行79文字までが結構いっぱいひっかかるので、--max-line-length=120を指定
$ flake8 . --filename="*.py" --statistics --max-line-length=120 ...省略... 1 E101 indentation contains mixed spaces and tabs 23 E111 indentation is not a multiple of four 65 E121 continuation line indentation is not a multiple of four 15 E122 continuation line missing indentation or outdented 42 E124 closing bracket does not match visual indentation 56 E125 continuation line does not distinguish itself from next logical line 138 E126 continuation line over-indented for hanging indent 113 E127 continuation line over-indented for visual indent 1078 E128 continuation line under-indented for visual indent 75 E201 whitespace after '{' 63 E202 whitespace before '}' 559 E203 whitespace before ',' 111 E221 multiple spaces before operator 14 E222 multiple spaces after operator 36 E225 missing whitespace around operator 7 E227 missing whitespace around bitwise or shift operator 4 E228 missing whitespace around modulo operator 414 E231 missing whitespace after ',' 310 E251 unexpected spaces around keyword / parameter equals 364 E261 at least two spaces before inline comment 3 E262 inline comment should start with '# ' 2 E271 multiple spaces after keyword 4 E272 multiple spaces before keyword 83 E301 expected 1 blank line, found 0 1718 E302 expected 2 blank lines, found 1 35 E303 too many blank lines (3) 3 E401 multiple imports on one line 328 E501 line too long (121 > 120 characters) 52 E502 the backslash is redundant between brackets 305 E701 multiple statements on one line (colon) 2 E702 multiple statements on one line (semicolon) 2 E703 statement ends with a semicolon 7 E711 comparison to None should be 'if cond is None:' 11 E712 comparison to False should be 'if cond is False:' or 'if not cond:' 2 E721 do not compare types, use 'isinstance()' 434 F401 'models' imported but unused 54 F403 'from django.contrib.auth.tests.custom_user import *' used; unable to detect undefined names 13 F811 redefinition of unused 'ContentType' from line 18 1 F812 list comprehension redefines 'cls' from line 59 17 F821 undefined name 'lookup_field' 66 F841 local variable 'lib' is assigned to but never used 3 F999 syntax error in doctest 1 W191 indentation contains tabs 296 W291 trailing whitespace 7 W292 no newline at end of file 18 W293 blank line contains whitespace 57 W391 blank line at end of file 3 W601 .has_key() is deprecated, use 'in'
思ったよりひっかかる