介绍

pycodestyle是一个工具,用于根据中的一些样式约定检查您的python代码。 PEP 8 .

特征

  • 插件架构:添加新的检查很容易。

  • 可解析输出:跳转到编辑器中的错误位置。

  • 小:只有一个python文件,只需要stdlib。你可以只使用 pycodestyle.py 为此目的归档。

  • 附带了一个全面的测试套件。

免责声明

该实用程序不会强制执行PEP8的每个规则。它有助于验证是否应用了某些编码约定,但并不打算详尽无遗。有些规则不能用一个简单的算法来表达,而其他规则只是您在需要时可以绕过的准则。

始终记住以下陈述: PEP 8

样式指南是关于一致性的。与此样式指南的一致性非常重要。项目内部的一致性更为重要。一个模块或功能内的一致性是最重要的。

除其他外,这些功能目前不在 pycodestyle 类库:

安装

您可以安装、升级、卸载 pycodestyle.py 使用以下命令:

$ pip install pycodestyle
$ pip install --upgrade pycodestyle
$ pip uninstall pycodestyle

示例用法和输出

$ pycodestyle --first optparse.py
optparse.py:69:11: E401 multiple imports on one line
optparse.py:77:1: E302 expected 2 blank lines, found 1
optparse.py:88:5: E301 expected 1 blank line, found 0
optparse.py:347:31: E211 whitespace before '('
optparse.py:357:17: E201 whitespace after '{'
optparse.py:472:29: E221 multiple spaces before operator

你也可以 pycodestyle.py 显示每个错误的源代码,甚至PEP 8中的相关文本:

$ pycodestyle --show-source --show-pep8 testing/data/E40.py
testing/data/E40.py:2:10: E401 multiple imports on one line
import os, sys
         ^
    Imports should usually be on separate lines.

    Okay: import os\nimport sys
    E401: import sys, os

或者,您可以显示发现每个错误的频率:

$ pycodestyle --statistics -qq Python-2.5/Lib
232     E201 whitespace after '['
599     E202 whitespace before ')'
631     E203 whitespace before ','
842     E211 whitespace before '('
2531    E221 multiple spaces before operator
4473    E301 expected 1 blank line, found 0
4006    E302 expected 2 blank lines, found 1
165     E303 too many blank lines (4)
325     E401 multiple imports on one line
3615    E501 line too long (82 characters)

你也可以 pycodestyle.py 使用显示不同格式的错误文本 --format 具有选项默认值/pylint/custom::

$ pycodestyle testing/data/E40.py --format=default
testing/data/E40.py:2:10: E401 multiple imports on one line

$ pycodestyle testing/data/E40.py --format=pylint
testing/data/E40.py:2: [E401] multiple imports on one line

$ pycodestyle testing/data/E40.py --format='%(path)s|%(row)d|%(col)d| %(code)s %(text)s'
testing/data/E40.py|2|10| E401 multiple imports on one line

中的变量 custom 格式选项

变量

意义

path

文件名

row

行号

col

列号

code

错误代码

text

错误文本

在命令行上提供快速帮助:

$ pycodestyle -h
Usage: pycodestyle [options] input ...

Options:
  --version            show program's version number and exit
  -h, --help           show this help message and exit
  -v, --verbose        print status messages, or debug with -vv
  -q, --quiet          report only file names, or nothing with -qq
  --first              show first occurrence of each error
  --exclude=patterns   exclude files or directories which match these comma
                       separated patterns (default: .svn,CVS,.bzr,.hg,.git)
  --filename=patterns  when parsing directories, only check filenames matching
                       these comma separated patterns (default: *.py)
  --select=errors      select errors and warnings (e.g. E,W6)
  --ignore=errors      skip errors and warnings (e.g. E4,W)
  --show-source        show source code for each error
  --show-pep8          show text of PEP 8 for each error (implies --first)
  --statistics         count errors and warnings
  --count              print total number of errors and warnings to standard
                       error and set exit code to 1 if total is not null
  --max-line-length=n  set maximum allowed line length (default: 79)
  --max-doc-length=n   set maximum allowed doc line length and perform these
                       checks (unchecked if not set)
  --indent-size=n      set how many spaces make up an indent (default: 4)
  --hang-closing       hang closing bracket instead of matching indentation of
                       opening bracket's line
  --format=format      set the error format [default|pylint|<custom>]
  --diff               report only lines changed according to the unified diff
                       received on STDIN

  Testing Options:
    --benchmark        measure processing speed

  Configuration:
    The project options are read from the [pycodestyle] section of the
    tox.ini file or the setup.cfg file located in any parent folder of the
    path(s) being processed.  Allowed options are: exclude, filename,
    select, ignore, max-line-length, max-doc-length, hang-closing, count,
    format, quiet, show-pep8, show-source, statistics, verbose.

    --config=path      user config file location
    (default: ~/.config/pycodestyle)

配置

行为可以配置为两个级别,即用户和项目级别。

在用户级别,从以下位置读取设置:

如果在Windows上:

~\.pycodestyle

否则,如果 XDG_CONFIG_HOME 定义环境变量:

XDG_CONFIG_HOME/pycodestyle

否则如果 XDG_CONFIG_HOME 未定义:

~/.config/pycodestyle

例子::

[pycodestyle]
count = False
ignore = E226,E302,E71
max-line-length = 160
statistics = True

在项目级别,a setup.cfg 文件或 tox.ini 如果存在,则读取文件。如果这些文件都没有 [pycodestyle] 节,未加载项目特定的配置。

错误代码

这是错误和警告代码的当前列表:

代码

样本消息

E1

缩进

E101

缩进包含混合空格和制表符

E111

缩进不是四的倍数

E112

应为缩进块

E113

意外缩进

E114

缩进不是四的倍数(注释)

E115

应为缩进块(注释)

E116

意外的缩进(注释)

E117

过度缩进

E121(**)

用于挂起缩进的缩进下的续行

E122(^)

续行缺少缩进或已过期

E123(*)

右括号与左括号线条的缩进不匹配

E124(^)

右括号与可视缩进不匹配

E125(^)

与下一个逻辑行具有相同缩进的续行

E126(**)

用于挂起缩进的连续行过缩进

E127(^)

连续行缩进过多,以便进行可视缩进

E128(^)

连续行在缩进下以进行可视缩进

E129(^)

与下一个逻辑行具有相同缩进的可视缩进行

E131(^)

连续行未对齐以挂起缩进

E133(*)

右括号缺少缩进

E2

空白空间

E201

'('后的空白

E202

“)之前的空格”

E203

‘,’,‘;’或‘:’前的空格

E211

'('前的空白

E221

运算符前有多个空格

E222

运算符后有多个空格

E223

运算符前的制表符

E224

接线员后的制表符

E225

运算符周围缺少空格

E226(*)

算术运算符周围缺少空格

E227

位或移位运算符周围缺少空格

E228

模运算符周围缺少空格

E23

“、'、';'或':'后缺少空格

E241(*)

“,”后有多个空格

E242(*)

后标签

E251

关键字/参数等号周围出现意外空格

E261

在内联注释之前至少有两个空格

E262

内联注释应以“”开头

E265

块注释应以“”开头

E266

块注释的前导“”太多

E27

关键字后有多个空格

E27

关键字前有多个空格

E27

关键字后的制表符

E27

关键字前的制表符

E27

关键字后缺少空格

E3

空白行

E301

应输入1空行,但找到0

E302

应输入2个空行,但找到0

E303

空行太多(3)

E304

在函数修饰符后发现空行

E305

函数或类结束后应输入2个空行

E306

嵌套定义前面应为1空行

E4

进口

E401

一行多个导入

E402

模块级导入不在文件顶部

E5

线长度

E501(^)

行太长(82个字符>79个字符)

E502

反斜杠在括号之间是多余的

E7

声明

E701

一行上有多个语句(冒号)

E702

一行上有多个语句(分号)

E703

语句以分号结尾

E704(*)

一行上有多个语句(def)

E711(^)

与none的比较应为“if cond is none:”

E712(^)

与true的比较应为“if cond is true:”或“if cond:”

E713

成员资格测试应为“不在”

E714

对象标识测试应为“is not”

E721(^)

不要比较类型,请使用“isInstance()”

E722

不要使用bare except,请改为指定exception

E731

不要指定lambda表达式,请使用def

E71

不要使用名为“l”、“o”或“i”的变量

E72

不要定义名为“l”、“o”或“i”的类

E73

不要定义名为“l”、“o”或“i”的函数

E9

运行时

E901

语法错误或缩进错误

E902

IOError

W1

缩进警告

W191

缩进包含制表符

W2

空白警告

W29

尾随空格

W29

文件结尾没有换行符

W29

空行包含空白

W3

空行警告

W39

文件结尾的空行

W5

断线警告

W503(*)

二元运算符前的换行符

W504(*)

二元运算符后的换行符

W505(*^)

文档行太长(82个字符>79个字符)

W6

弃用警告

W605

转义序列'x'无效

(*) 在默认配置中,检查 E121E123E126E133E226E241E242E704W503W504W505 被忽视,因为它们不是一致接受的规则,并且 PEP 8 不会强制执行它们。请注意,如果选项 --ignore=errors 则默认配置将被覆盖,并且仅忽略您跳过的复选标记(S)。这张支票 W503 与CHECK互斥 W504 。这张支票 E133 与CHECK互斥 E123 。使用开关 --hang-closing 去报道 E133 而不是 E123 。使用开关 --max-doc-length=n 去报道 W505

**(^)**可以在行级别使用 # noqa 特别评论。这种可能性应该保留在特殊情况下。

特殊情况不足以打破规则。

注:大多数错误都可以用这样的一行代码列出:

$ python pycodestyle.py --first --select E,W testing/data --format '%(code)s: %(text)s'