今天写Python脚本时遇到一个问题,重构代码时,修改了一个变量名, 但只改了正常分支中的,忘了改错误处理分支中的,由于没有Linter做未定义变量检查, bug在本机测试时并没有被发现,直到被push到remote repo, 在Jenkins job里运行几次出错后才被发现。 解决方案是安装flake8以及vim插件。
Install it with conda install -c <my-env> flake8
.
命令行中检查文件命令:flake8 syncRecurrences.py
.
与 vim 的整合
通过 ALE 插件整合参考 dsnote Python IDE based on vim.
通过 syntastic 插件整合:
let g:syntastic_python_python_exec = '/usr/bin/python3'
let g:syntastic_python_flake8_exec = 'python3'
let g:syntastic_python_flake8_args = ['-m', 'flake8']
配置好后用vim打开python脚本,保存文件后,在未声明的变量行上会出现下面的错误信息:
undefined name 'objId' [F821]
错误代码列表见 Warning / Error codes of flake8.
其中 E
, W
(分别代表 错误 和 警告)开头的是 PEP 8 定义的,
具体说明见 Error codes.
Configurations
See Configuring Flake8 for details.
User level config
$ cat ~/.config/.flake8
[flake8]
ignore = E501
Project level config
flake8 will search for tox.ini (recommended), setup.cfg and .flake8 for the configuration.
$ cat $PROJ_HOME/tox.ini
[flake8]
ignore = E501
File/Line level ignore list
Ignoring entire files: adding # flake8: noqa
to the file.
Ignore errors for a specific line:
-
Ignore some errors: append comment
# noqa: <ignore-list>
at the line, for example:# noqa: E731,E123
. -
Ignore all errors: append comment
# noqa
at the line.