为了账号安全,请及时绑定邮箱和手机立即绑定

Python 常用静态代码检查工具简介

标签:
Python

对于我这种习惯了 Java 这种编译型语言,在使用 Python 这种动态语言的时候,发现错误经常只能在执行的时候发现,总感觉有点不放心。

而且有一些错误由于隐藏的比较深,只有特定逻辑才会触发,往往导致需要花很多时间才能将语法错误慢慢排查出来。其实有一些错误是很明显的,假如能在写程序的时候发现这些错误,就能提高工作效率。

这时候 Python 静态语法检查工具就出现了。

本文使用之前文章Python 助你填写高考志愿中的代码作为测试代码。另外有些输出过长的,进行了截取。

pep8/pycodestyle

相信大家多多少少都见过 PEP 8,那 PEP 8 到底是个啥?

其实 PEP 8 是一种 Python 代码规范指南,可以参阅官网:https://www.python.org/dev/peps/pep-0008/,其目的是为了保持代码的一致性、可读性。

检查自己代码是否符合 PEP 8 规范,一个简单的工具就是:pep8。

安装
$ pip install pep8

在使用时发现 pep8 给出了一个警告:

$ pep8 gkcx.py
/usr/local/lib/python3.5/dist-packages/pep8.py:2124: UserWarning:

pep8 has been renamed to pycodestyle (GitHub issue #466)
Use of the pep8 tool will be removed in a future release.
Please install and use `pycodestyle` instead.

$ pip install pycodestyle
$ pycodestyle ...

  '\n\n'

意思是 pep8 已被 pycodestyle 替代!

使用

基本使用方法:$ pycodestyle [file name or directory name]

$ pycodestyle gkcx.py
gkcx.py:11:80: E501 line too long (135 > 79 characters)
gkcx.py:14:1: E302 expected 2 blank lines, found 1
gkcx.py:15:80: E501 line too long (94 > 79 characters)
...省略部分
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:71:80: E501 line too long (100 > 79 characters)
gkcx.py:82:25: E231 missing whitespace after ','
gkcx.py:82:29: E231 missing whitespace after ','
gkcx.py:84:1: W293 blank line contains whitespace
gkcx.py:84:1: W391 blank line at end of file
  • 参数 --statistics -qq :对结果进行汇总
$ pycodestyle gkcx.py --statistics -qq
3       E203 whitespace before ':'
1       E225 missing whitespace around operator
16      E231 missing whitespace after ':'
3       E302 expected 2 blank lines, found 1
1       E305 expected 2 blank lines after class or function definition, found 1
6       E501 line too long (135 > 79 characters)
1       W291 trailing whitespace
1       W293 blank line contains whitespace
1       W391 blank line at end of file
  • 参数 --show-source:更详细的输出
$ pycodestyle gkcx.py --show-source
gkcx.py:14:1: E302 expected 2 blank lines, found 1
def get_school_id(school_name):
^
gkcx.py:15:80: E501 line too long (94 > 79 characters)
    Referer = "https://gkcx.eol.cn/soudaxue/queryschool.html?&keyWord1={}".format(school_name)
                                                                               ^
gkcx.py:18:19: E203 whitespace before ':'
        "messtype" : "jsonp",
                  ^
gkcx.py:19:12: E231 missing whitespace after ':'
        "_":"1530074932531",
           ^
...省略部分           
  • 参数 --ignore:忽略指定输出
$ pycodestyle gkcx.py --ignore=E225,E501,E231
gkcx.py:14:1: E302 expected 2 blank lines, found 1
gkcx.py:18:19: E203 whitespace before ':'
gkcx.py:20:19: E203 whitespace before ':'
gkcx.py:21:19: E203 whitespace before ':'
gkcx.py:32:1: E302 expected 2 blank lines, found 1
gkcx.py:41:1: E302 expected 2 blank lines, found 1
gkcx.py:55:15: W291 trailing whitespace
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:84:1: W293 blank line contains whitespace
gkcx.py:84:1: W391 blank line at end of file
错误码含义
  • E...:错误
  • W...:警告
  • 100 型:缩进问题
  • 200 型:空格问题
  • 300 型:空行问题
  • 400 型:导入问题
  • 500 型:行长度问题
  • 600 型:已弃用
  • 700 型:声明问题
  • 900 型:语法错误
Pyflakes

一个用于检查 Python 源文件错误的简单程序。

Pyflakes 分析程序并且检查各种错误。它通过解析源文件实现,无需导入它,因此在模块中使用是安全的,没有任何的副作用。

  • 不会检查代码风格
  • 由于它是单独检查各个文件,因此它也相当的快,当然检测范围也有一定的局限
安装
pip install pyflakes
使用

$ pyflakes [ file name or directory name]

$ pyflakes gkcx.py
gkcx.py:3: 'bs4.BeautifulSoup' imported but unused
gkcx.py:6: 're' imported but unused
Pylint

PyLint 是 Python 源代码分析器,可以分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码,是一个可以用于验证多个文件的模块和包的工具。

缺省情况下,PyLint 启用许多规则。它具有高度可配置性,从代码内部处理程序控制它。另外,编写插件添加到自己的检查中是可能的。

安装
$ pip install pylint

$ pylint --version
pylint 2.0.0
astroid 2.0.1
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
使用

基本使用:pylint [options] module_or_package

$ pylint gkcx.py
************* Module gkcx
gkcx.py:11:0: C0301: Line too long (135/100) (line-too-long)
gkcx.py:25:47: C0326: Exactly one space required after comma
    response = requests.request("GET", data_url,headers=headers,params=params)
                                               ^ (bad-whitespace)
gkcx.py:36:0: C0301: Line too long (113/100) (line-too-long)
gkcx.py:1:0: C0111: Missing module docstring (missing-docstring)
gkcx.py:10:0: C0103: Constant name "headers" doesn't conform to UPPER_CASE naming style (invalid-name)
...省略部分
gkcx.py:14:18: W0621: Redefining name 'school_name' from outer scope (line 68) (redefined-outer-name)
gkcx.py:32:0: C0111: Missing function docstring (missing-docstring)
gkcx.py:33:4: W0622: Redefining built-in 'id' (redefined-builtin)
gkcx.py:32:12: W0621: Redefining name 'school' from outer scope (line 72) (redefined-outer-name)

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

发现 Pylint 还会给代码整体打一个分数,我们就可以根据提示一步步调优,提高分数!10 分满分。

如果运行两次 Pylint,它会同时显示出当前和上次的运行结果,从而可以看出代码质量是否得到了改进。

错误代码含义
  • C:惯例,违反了编码风格标准
  • R:重构,代码非常糟糕
  • W:警告,某些 Python 特定的问题
  • E:错误,很可能是代码中的错误
  • F:致命错误,阻止 Pylint 进一步运行的错误
flake8

Flake8 是由 Python 官方发布的一款辅助检测 Python 代码是否规范的工具,相对于目前热度比较高的 Pylint 来说,Flake8 检查规则灵活,支持集成额外插件,扩展性强。Flake8 是对下面三个工具的封装:

  1. PyFlakes:静态检查 Python 代码逻辑错误的工具。
  2. Pep8: 静态检查 PEP8 编码风格的工具。
  3. NedBatchelder’s McCabe :静态分析 Python 代码复杂度的工具。

不光对以上三个工具的封装,Flake8还提供了扩展的开发接口。

官方文档:https://pypi.python.org/pypi/flake8/

安装
$ pip install flake8       

$ flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.5.2 on Linux
使用

基本使用方法:flake8 [file name or directory name]

$ flake8 gkcx.py
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
gkcx.py:11:80: E501 line too long (135 > 79 characters)
gkcx.py:14:1: E302 expected 2 blank lines, found 1
...省略部分
gkcx.py:71:80: E501 line too long (100 > 79 characters)
gkcx.py:82:25: E231 missing whitespace after ','
gkcx.py:82:29: E231 missing whitespace after ','
gkcx.py:84:1: W293 blank line contains whitespace
gkcx.py:84:1: W391 blank line at end of file

PyFlakes 和 Pep8 的输出将合并起来一起返回。可以看出 flake8 不止检查代码错误,还会对代码规范不对的地方进行检查,比如:一行代码过长。

Flake8 提供一个扩展选项:--max-complexity,如果函数的 McCabe 复杂度比给定的值更高将发出一个告警。该功能对于发现代码过度复杂非常有用,根据 Thomas J. McCabe, Sr 研究,代码复杂度不宜超过 10,而 Flake8 官网建议值为 12。

  • McCabe 复杂度默认情况下是不会输出的,需要通过 --max-complexity 指定:
$ flake8 gkcx.py --max-complexity=5
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
...省略部分
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:67:1: C901 'If 67' is too complex (6)
gkcx.py:71:80: E501 line too long (100 > 79 characters)
gkcx.py:82:25: E231 missing whitespace after ','
gkcx.py:82:29: E231 missing whitespace after ','
gkcx.py:84:1: W391 blank line at end of file
gkcx.py:84:1: W293 blank line contains whitespace
  • 可以通过 --ignore 忽略指定输出:
$ flake8 gkcx.py --ignore E501,E231,E203
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
gkcx.py:14:1: E302 expected 2 blank lines, found 1
gkcx.py:32:1: E302 expected 2 blank lines, found 1
gkcx.py:38:22: E225 missing whitespace around operator
gkcx.py:41:1: E302 expected 2 blank lines, found 1
gkcx.py:55:15: W291 trailing whitespace
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:84:1: W391 blank line at end of file
gkcx.py:84:1: W293 blank line contains whitespace
  • 通过 --select 参数设置只展示指定输出:
$ flake8 gkcx.py --select F401
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
错误码含义

Flake8 基础错误返回码一共有三类:

  • E***/W***:PEP8 中的 error 和 warning。
  • F***:通过 PyFlakes 检测出的 error,其实 PyFlakes 本身是不提供错误返回码的,flake8 对 pyflakes 返回的错误消息进行了分类。
  • C9**:通过 McCabe 检测出的代码复杂度。
总结

Python 静态代码检查工具不止这几个,大家可以挑选合适的进行使用。本人也没有进行深入地探究,有兴趣进行高阶使用的可以参照官网。

另外,各个工具应该都有对应的插件,比如 vim、vscode、eclipse、pycharm 上应该都能集成上述工具,大家可以网上找下适合自己 ide 的插件进行安装。

有些人估计看到那么多异常也会烦,但是毕竟是可以培养大家代码规范的,后续工作后,肯定也有相应的代码规范,建议大家养成习惯。

本文原创发布于慕课网 ,转载请注明出处,谢谢合作

点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消