【解锁】flake8——python官方代码扫描工具

传送门

python官方代码扫描工具

Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,flake8是下面三个工具的封装:

  1. PyFlakes
  2. Pep8
  3. NedBatchelder’s McCabe script

概要

flake8 [options] [<path> <path> ...]

flake8 --help

选项

$ flake8 --help
Usage: flake8 [options] file file ...

Options:
  --version             show program s version number and exit
  -h, --help            show this help message and exit
  -v, --verbose         Print more information about what is happening in
                        flake8. This option is repeatable and will increase
                        verbosity each time it is repeated.
  -q, --quiet           Report only file names, or nothing. This option is
                        repeatable.
  --count               Print total number of errors and warnings to standard
                        error and set the exit code to 1 if total is not
                        empty.
  --diff                Report changes only within line number ranges in the
                        unified diff provided on standard in by the user.
  --exclude=patterns    Comma-separated list of files or directories to
                        exclude. (Default:
                        .svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg)
  --filename=patterns   Only check for filenames matching the patterns in this
                        comma-separated list. (Default: *.py)
  --stdin-display-name=STDIN_DISPLAY_NAME
                        The name used when reporting errors from code passed
                        via stdin. This is useful for editors piping the file
                        contents to flake8. (Default: stdin)
  --format=format       Format errors according to the chosen formatter.
  --hang-closing        Hang closing bracket instead of matching indentation
                        of opening bracket's line.
  --ignore=errors       Comma-separated list of errors and warnings to ignore
                        (or skip). For example, ``--ignore=E4,E51,W234``.
                        (Default: E121,E123,E126,E226,E24,E704,W503,W504)
  --extend-ignore=errors
                        Comma-separated list of errors and warnings to add to
                        the list of ignored ones. For example, ``--extend-
                        ignore=E4,E51,W234``.
  --per-file-ignores=PER_FILE_IGNORES
                        A pairing of filenames and violation codes that
                        defines which violations to ignore in a particular
                        file. The filenames can be specified in a manner
                        similar to the ``--exclude`` option and the violations
                        work similarly to the ``--ignore`` and ``--select``
                        options.
  --max-line-length=n   Maximum allowed line length for the entirety of this
                        run. (Default: 79)
  --max-doc-length=n    Maximum allowed doc line length for the entirety of
                        this run. (Default: none)
  --select=errors       Comma-separated list of errors and warnings to enable.
                        For example, ``--select=E4,E51,W234``. (Default:
                        E,F,W,C90)
  --disable-noqa        Disable the effect of "# noqa". This will report
                        errors on lines with "# noqa" at the end.
  --show-source         Show the source generate each error or warning.
  --statistics          Count errors and warnings.
  --enable-extensions=ENABLE_EXTENSIONS
                        Enable plugins and extensions that are otherwise
                        disabled by default
  --exit-zero           Exit with status code "0" even if there are errors.
  --install-hook=INSTALL_HOOK
                        Install a hook that is run prior to a commit for the
                        supported version control system.
  -j JOBS, --jobs=JOBS  Number of subprocesses to use to run checks in
                        parallel. This is ignored on Windows. The default,
                        "auto", will auto-detect the number of processors
                        available to use. (Default: auto)
  --output-file=OUTPUT_FILE
                        Redirect report to a file.
  --tee                 Write to stdout and output-file.
  --append-config=APPEND_CONFIG
                        Provide extra config files to parse in addition to the
                        files found by Flake8 by default. These files are the
                        last ones read and so they take the highest precedence
                        when multiple files provide the same option.
  --config=CONFIG       Path to the config file that will be the authoritative
                        config source. This will cause Flake8 to ignore all
                        other configuration files.
  --isolated            Ignore all configuration files.
  --benchmark           Print benchmark information about this run of Flake8
  --bug-report          Print information necessary when preparing a bug
                        report
  --builtins=BUILTINS   define more built-ins, comma separated
  --doctests            check syntax of the doctests
  --include-in-doctest=INCLUDE_IN_DOCTEST
                        Run doctests only on these files
  --exclude-from-doctest=EXCLUDE_FROM_DOCTEST
                        Skip these files when running doctests
  --max-complexity=MAX_COMPLEXITY
                        McCabe complexity threshold

Installed plugins: mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1

例子

只需对当前目录运行flake8:

flake8
flake8 .

针对特定路径运行flake8:

flake8 path/to/file.py

忽略来自flake8的违规行为:

flake8 --ignore E101
flake8 --ignore E1,E202

仅报告某些违规行为:

flake8 --select E101
flake8 --select E2,E742

仅分析差异:

git diff -U0 | flake8 --diff -

生成错误报告的信息:

flake8 --bug-report
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏 克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值