Pylint 最佳实践

发布于 2024-10-06 10:42:12 字数 122 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光病人 2024-10-13 10:42:12

您可以通过以下方式阻止任何您不喜欢的警告/错误:

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...).

不及他 2024-10-13 10:42:12

要永久禁用警告和约定:

  1. 通过运行 pylint --generate-rcfile > 创建一个 ~/.pylintrc 文件。 ~/.pylintrc
  2. 编辑 ~/.pylintrc
  3. 取消注释 disable= 并将该行更改为 disable=W,C

To persistently disable warnings and conventions:

  1. Create a ~/.pylintrc file by running pylint --generate-rcfile > ~/.pylintrc
  2. Edit ~/.pylintrc
  3. Uncomment disable= and change that line to disable=W,C
深爱不及久伴 2024-10-13 10:42:12

Pyflakes 应该可以很好地满足您的目的。

Pyflakes should serve your purpose well.

安稳善良 2024-10-13 10:42:12

-E 只会标记 Pylint 认为的错误(即没有警告、没有约定等)

-E will only flag what Pylint thinks is an error (i.e., no warnings, no conventions, etc.)

○愚か者の日 2024-10-13 10:42:12

使用 grep 如下:

pylint my_file.py | grep -v "^C"

编辑:
正如问题中提到的,要从 pylint 输出中删除约定建议,请删除以大写 C 开头的行。

pylint 的文档,输出由符合格式的行组成

MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE

,消息类型可以是:

  • [R]efactor for a “goodpractice” Metric Violation
  • [C]onvention违反编码标准
  • [W] 警告文体问题或次要编程问题
  • [E] 错误 重要编程问题(即最有可能的错误)
  • [F] atal 阻止进一步处理的错误

仅显示第一个字母,因此您可以使用 grep 来选择/删除您想要的消息类型的级别。

我最近没有使用 Pylint,但我可能会使用 Pylint 内部的参数来执行此操作。

Using grep like:

pylint my_file.py | grep -v "^C"

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

MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE

and the message type can be:

  • [R]efactor for a “good practice” metric violation
  • [C]onvention for coding standard violation
  • [W]arning for stylistic problems, or minor programming issues
  • [E]rror for important programming issues (i.e. most probably bug)
  • [F]atal for errors which prevented further processing

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文