Pylint 最佳实践
Pylint 看起来是一个运行 Python 代码分析的好工具。
然而,我们的主要目标是捕获任何潜在的错误,而不是编码约定。启用所有 Pylint 检查似乎会产生大量噪音。您使用并有效的 Pylint 功能集是什么?
Pylint looks like a good tool for running analysis of Python code.
However, our main objective is to catch any potential bugs and not coding conventions. Enabling all Pylint checks seems to generate a lot of noise. What is the set of Pylint features you use and is effective?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过以下方式阻止任何您不喜欢的警告/错误:
pylint --disable=error1,error2
我已阻止以下内容(来自 http://www.logilab.org/card/pylintfeatures):
W0511:当检测到 FIXME 或 XXX 等警告注释时使用
W0142:使用*或*魔法*。当使用
*args
或**kwargs
调用函数或方法来分派参数时使用。这不会提高可读性,应谨慎使用。W0141:使用了内置函数 %r。当使用列入黑名单的内置函数时使用(请参阅 bad-function 选项)。通常的黑名单函数是诸如映射或过滤器之类的函数,Python 现在提供了一些更清晰的替代方案,例如列表理解。
R0912: 分支过多 (%s/%s)。当函数或方法有太多分支而难以理解时使用。
R0913:参数太多(%s/%s)。当函数或方法接受太多参数时使用。
R0914: 局部变量过多 (%s/%s)。当函数或方法有太多局部变量时使用。
R0903:公共方法太少(%s/%s)。当类的公共方法太少时使用,因此请确保它确实值得。
W0212: 访问客户端类的受保护成员 %s。当受保护成员(即名称以下划线开头的类成员)在类外部或定义它的类的后代进行访问时使用。
W0312:发现缩进是 %ss 而不是 %ss。当模块中存在一些混合制表符和空格时使用。
C0111:缺少文档字符串。当模块、函数、类或方法没有文档字符串时使用。一些特殊方法(例如
__init__
)不一定需要文档字符串。C0103:名称“%s”无效(应匹配 %s)。当名称与与其类型(常量、变量、类...)关联的正则表达式不匹配时使用。
You can block any warnings/errors you don't like, via:
pylint --disable=error1,error2
I've blocked the following (description from http://www.logilab.org/card/pylintfeatures):
W0511: Used when a warning note as FIXME or XXX is detected
W0142: Used * or * magic*. Used when a function or method is called using
*args
or**kwargs
to dispatch arguments. This doesn't improve readability and should be used with care.W0141: Used builtin function %r. Used when a black listed builtin function is used (see the bad-function option). Usual black listed functions are the ones like map, or filter, where Python offers now some cleaner alternative like list comprehension.
R0912: Too many branches (%s/%s). Used when a function or method has too many branches, making it hard to follow.
R0913: Too many arguments (%s/%s). Used when a function or method takes too many arguments.
R0914: Too many local variables (%s/%s). Used when a function or method has too many local variables.
R0903: Too few public methods (%s/%s). Used when class has too few public methods, so be sure it's really worth it.
W0212: Access to a protected member %s of a client class. Used when a protected member (i.e. class member with a name beginning with an underscore) is access outside the class or a descendant of the class where it's defined.
W0312: Found indentation with %ss instead of %ss. Used when there are some mixed tabs and spaces in a module.
C0111: Missing docstring. Used when a module, function, class or method has no docstring. Some special methods like
__init__
don't necessarily require a docstring.C0103: Invalid name "%s" (should match %s). Used when the name doesn't match the regular expression associated to its type (constant, variable, class...).
要永久禁用警告和约定:
pylint --generate-rcfile > 创建一个
~/.pylintrc
文件。 ~/.pylintrc~/.pylintrc
disable=
并将该行更改为disable=W,C
To persistently disable warnings and conventions:
~/.pylintrc
file by runningpylint --generate-rcfile > ~/.pylintrc
~/.pylintrc
disable=
and change that line todisable=W,C
Pyflakes 应该可以很好地满足您的目的。
Pyflakes should serve your purpose well.
-E 只会标记 Pylint 认为的错误(即没有警告、没有约定等)
-E will only flag what Pylint thinks is an error (i.e., no warnings, no conventions, etc.)
使用 grep 如下:
编辑:
正如问题中提到的,要从 pylint 输出中删除约定建议,请删除以大写 C 开头的行。
从 pylint 的文档,输出由符合格式的行组成
,消息类型可以是:
仅显示第一个字母,因此您可以使用 grep 来选择/删除您想要的消息类型的级别。
我最近没有使用 Pylint,但我可能会使用 Pylint 内部的参数来执行此操作。
Using grep like:
Edit :
As mentionned in the question, to remove the conventions advices from pylint output, you remove the lines that start with an uppercase C.
From the doc of pylint, the output consists in lines that fit the format
and the message type can be:
Only the first letter is displayed, so you can play with grep to select/remove the level of message type you want.
I didn't use Pylint recently, but I would probably use a parameter inside Pylint to do so.