python开源系列文章
代码规范
- PEP8
- 谷歌代码规范
代码检查
- flake8
- pylint
- mypy
- pyright
flake操作可以参考这了:https://github.com/tensorflow/addons/blob/master/tools/format.py
# 检查整个项目:
flake8 path/to/your_project/
# 检查单个文件:
flake8 path/to/your_file.py
其中,可以在命令行上进行配置,也可以使用配置文件,配置文件可以在最高级的用户目录下,或者当前目录下,setup.cfg,tox.ini, 或者.flake8 文件都可以,文件需要以 [flake8] 段落开头:
[flake8]
max-line-length = 120
show-source = true
pre-commit
实际项目可以通过pre-commit ,在执行特定 git 操作的时候运行这些脚本。例如,在 commit或者 push之前运行一下 flake8、 bandit和 safety这些工具。
参考官方文档中的quick start
1 Install pre-commit : pip install pre-commit
2 Add a pre-commit configuration
3 用 pre-commit install安装git hooks到你的 .git/目录, pre-commit install
4. 运行 pre-commit run --all-files
若检测出错,则无法commit,也就无法push,保证了出错代码只在本地,不会把问题提交到远程仓库
- 如果不想要commit之前检查,直接把以下文件删除。因为有时候实在跑不通
rm .git/hooks/pre-commit
Mypy: Optional Static Typing for Python
另外,比较新的也可以使用mypy进行检查
参考
- https://zhuanlan.zhihu.com/p/400870942
- https://zhuanlan.zhihu.com/p/250493093
- https://zhuanlan.zhihu.com/p/94008510
- https://zhuanlan.zhihu.com/p/65820736
- https://www.zhihu.com/people/hbaaron/posts