用flake8检查python代码

2020年12月1日 / 914次阅读 / Last Modified 2020年12月4日

flake8是python代码的静态检查(lint)工具,它汇集了好几个其它工具,提供统一的使用接口。很多人都说,它比pylint好用。

flake8的安装:

$ python3 -m pip install --user flake8
$ flake8 --help

对py文件做lint检查:

$ flake8 path/to/xxxx.py
$ flake8 path/to/code/
# or
$ flake8

从名字上看,就知道flake8是根据Python的PEP8代码规范做检查。除此之外,我比较看重的一个feature是,flake8可以检查undefined name。

Python是解释执行的代码,代码在执行前,制作syntax检查,而对于undefined name,只有代码执行到那个位置,才能被发现。这就导致对python代码的测试,要尽可能做到100%的分支覆盖,增加了测试的难度。flake8可以做undefined name检查!

$ flake8 test.py
common.py:508:5: F821 undefined name 'fprint'

由于大部分时候,flake8的输出都很多(检查我的代码总是这样...),你可以用 --select 指定只检查某些类型的问题,比如只检查 F821 undefined name错误:

$ flake8 --select F821 common.py
common.py:508:5: F821 undefined name 'fprint'

或者 select 某类violation,下面的示例,只显示F类:

$ flake8 --select F common.py
common.py:508:5: F821 undefined name 'fprint'

也是因为有的时候输出太多,以一些告警信息可能是不需要的,可以用 --ignore 参数将其忽略:

$ flake8 --ignore W391,E231,E226,E225 common.py
common.py:45:34: E228 missing whitespace around modulo operator
common.py:98:16: E127 continuation line over-indented for visual indent
common.py:117:7: E221 multiple spaces before operator
common.py:119:7: E221 multiple spaces before operator
common.py:147:5: E722 do not use bare 'except'
common.py:168:5: E722 do not use bare 'except'
common.py:238:21: E126 continuation line over-indented for hanging indent
common.py:239:27: E126 continuation line over-indented for hanging indent
common.py:326:24: E228 missing whitespace around modulo operator
common.py:445:76: E228 missing whitespace around modulo operator
common.py:477:22: E127 continuation line over-indented for visual indent
common.py:491:26: E228 missing whitespace around modulo operator
common.py:508:5: F821 undefined name 'fprint'

上面的命令还可以写成这样:

$ flake8 --ignore W391,E231,E22 common.py

E22表示忽略掉所有E22开头的violations。

--ignore 是 only 的意思,表示只忽略XXX,如果你有一个tox.ini(下面有介绍),里面配置了一些ignore,当在命令行再使用 --ignore 的时候,tox.ini中的ignore就失效了。

此时,可以考虑使用 --extend-ignore ,它表示在tox.ini已经配置的基础上,追加!

--exclude,用来批量检查的时候,忽略指定的文件:

$ flake8 *.py --exclude common.py,test.py

上面的命令,检查当前目录下的所有.py文件,但是忽略掉其中两个。

--format,用来定义输出格式:

$ flake8 --format='%(path)s: %(row)d,%(col)d: %(code)s: %(text)s' common.py

--statistics,查看各种告警的统计信息,一般与 --quiet 参数一起使用,抑制具体的violation信息:

$ flake8 --statistics --quiet common.py
common.py
2     E127 continuation line over-indented for visual indent
2     E221 multiple spaces before operator
4     E225 missing whitespace around operator
4     E228 missing whitespace around modulo operator
25    E231 missing whitespace after ','
2     E722 do not use bare 'except'
1     F821 undefined name 'fprint'
1     W391 blank line at end of file

--count,统计一个总数,示例略,也是通常与 --quiet 一起使用!

--max-complexity,代码复杂度检查,通过这个参数设一个阈值,超过的将显示出来,默认是12!这个功能也是我很喜欢的...

$ flake8  common.py --max-complexity 4 --select C
common.py:203:1: C901 'get_ipv4_pair' is too complex (5)
common.py:254:1: C901 'get_mac_pair' is too complex (5)
common.py:306:1: C901 'eStr' is too complex (5)
common.py:449:1: C901 'trecv_all' is too complex (5)
common.py:476:1: C901 'cprint' is too complex (8)

tox.ini 配置文件

项目文件夹中的tox.ini文件,可以作为flake8的配置文件,将配置写入文件,就不用频繁输入了。下面是个tox.ini文件格式示意:

$ cat tox.ini
[flake8]
format = %(path)s: %(row)d,%(col)d: %(code)s: %(text)s
ignore = E225,E226,E231,W391,E722
max-complexity = 10
exclude = .git,__pycache__,README.md,LICENSE

tox.ini文件格式符合ini格式。(python自带的configparser模块,就是处理ini格式的)

如果你不喜欢tox.ini(tox是毒药的意思)这个名称,可以随意取名,然后用 --config 指定这个ini文件即可。

在tox.ini文件中,可以配置单文件级的ignore,如下:

$ cat tox.ini
[flake8]
per-file-igores = 
    test.py: F403,F405

-- EOF --

本文链接:https://www.pynote.net/archives/2930

相关文章

    留言区

    《用flake8检查python代码》有2条留言

    您的电子邮箱地址不会被公开。 必填项已用*标注

    • 麦新杰

      flake8能够发现重复的import [回复]

    • 麦新杰

      flake8文档:https://flake8.pycqa.org/en/latest/index.html [回复]


    前一篇:
    后一篇:

    More

    麦新杰的Python笔记

    Ctrl+D 收藏本页


    ©Copyright 麦新杰 Since 2019 Python笔记

    go to top