dlint是一个鼓励最佳编码实践并帮助确保我们编写安全的python代码的工具。
dlint的Python项目详细描述
dlint
dlint是一个鼓励最佳编码实践并帮助确保我们 编写安全的python代码。
The most important thing I have done as a programmer in recent years is to aggressively pursue static code analysis. Even more valuable than the hundreds of serious bugs I have prevented with it is the change in mindset about the way I view software reliability and code quality.
For a static analysis project to succeed, developers must feel they benefit from and enjoy using it.
文档:请参见docs。
安装
$ python -m pip install dlint
指定要为特定python版本安装的python2
或python3
。
并再次检查其安装是否正确:
$ python -m flake8 -h
Usage: flake8 [options] file file ...
...
Installed plugins: dlint: 0.7.0, mccabe: 0.5.3, pycodestyle: 2.2.0, pyflakes: 1.3.0
注意dlint: 0.7.0
。
使用
dlint使用flake8
来执行其linting功能。这样我们就可以
利用许多有用的flake8
功能,而不必重新发明轮子。
cli
让我们做一个简单的检查:
$ cat test.py
#!/usr/bin/env python
print("TEST1")
exec('print("TEST2")')
$ python -m flake8 --select=DUO test.py
test.py:5:1: DUO105 use of "exec" not allowed
--select=DUO
标志告诉flake8
只运行dlint lint规则。
从这里,我们可以轻松地对python代码目录运行dlint:
$ python -m flake8 --select=DUO /path/to/code
要微调您的绒线,请查看flake8
帮助:
$ python -m flake8 --help
内联编辑器
dlint结果也可以内联地包含在编辑器中,以获得快速反馈。 这通常需要一个编辑器插件或扩展。这里是一些开始 常用编辑器要点:
- 波形学:https://github.com/vim-syntastic/syntastic
- emacs:https://github.com/flycheck/flycheck
- 崇高:https://github.com/SublimeLinter/SublimeLinter-flake8
- Pycharm:https://foxmask.net/post/2016/02/17/pycharm-running-flake8/
- 原子:https://atom.io/packages/linter-flake8
- Visual Studio代码:https://code.visualstudio.com/docs/python/linting#_flake8
积分
dlint可以很容易地集成到ci管道中,或者任何真正的东西中。
特拉维西
在配置文件中包含dlint:
language: python
install:
- python -m pip install dlint
script:
- python -m flake8 --select=DUO /path/to/code
圆圈
在配置文件中包含dlint:
version: 2
jobs:
build:
docker:
- image: circleci/python
steps:
- checkout
- run: python -m pip install dlint
- run: python -m flake8 --select=DUO /path/to/code
Gitlab
在配置文件中包含dlint:
stages:
- test
test:
image: python
before_script:
- python -m pip install dlint
script:
- python -m flake8 --select=DUO /path/to/code
锅炉
在Arcanist中包含dlint
通过^{
{
"linters": {
"sample": {
"type": "flake8"
}
}
}
dlint规则在安装后将通过flake8
自动运行,因此
标准配置将起作用。你也可以利用更多的颗粒
对起毛过程的控制:
{
"linters": {
"sample": {
"type": "flake8"
},
"bin": ["python2.7", "python2"],
"flags": ["-m", "flake8", "--select", "DUO"]
}
}
自定义插件
dlint的自定义插件构建在simple naming convention, 依赖于Python modules。 要制作dlint自定义插件,请使用以下约定:
- python模块名必须以
dlint_plugin_
开头。 - linter类名必须以
Dlint
开头。 - linter类应该从
dlint.linters.base.BaseLinter
继承。- 如果出于某种原因您希望避免这种情况,那么必须实现
get_results
函数正确,并且继承自ast.NodeVisitor
。
- 如果出于某种原因您希望避免这种情况,那么必须实现
有关详细信息,请参见example plugin。
开发
首先,安装开发包:
$ python -m pip install -r requirements.txt
$ python -m pip install -r requirements-dev.txt
$ python -m pip install -e .
测试
$ pytest
起毛
$ flake8
覆盖范围
$ pytest --cov