Python PyLint 最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4395499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:33:56  来源:igfitidea点击:

PyLint Best Practices?

pythonstatic-analysispylint

提问by amit

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 convention. Enabling all pyLint check seems to generate lot of noise. Any suggestions on a set of pyLint features you use and is effective?

pyLint 看起来是一个运行 Python 代码分析的好工具。然而,我们的主要目标是捕捉任何潜在的错误,而不是编码约定。启用所有 pyLint 检查似乎会产生很多噪音。对您使用的一组 pyLint 功能有什么建议并且有效吗?

回答by user225312

Pyflakesshould serve your purpose well.

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

回答by muckabout

You can block any warnings/errors you don't like, via:

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

pylint --disable=error1,error2

pylint --disable=error1,error2

I've blocked the following (description from http://www.logilab.org/card/pylintfeatures):

我已经阻止了以下内容(来自http://www.logilab.org/card/pylintfeatures 的描述):

W0511: Used when a warning note as FIXME or XXX is detected

W0511:在检测到 FIXME 或 XXX 警告注释时使用

W0142: Used * or * magic* Used when a function or method is called using *argsor **kwargsto dispatch arguments. This doesn't improve readability and should be used with care.

W0142: Used * or * magic* 在使用*args**kwargs调度参数调用函数或方法时使用。这不会提高可读性,应谨慎使用。

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.

W0141:使用的内置函数 %r 在使用列入黑名单的内置函数时使用(请参阅错误功能选项)。通常的黑名单函数是像 map 或 filter 这样的函数,Python 现在提供了一些更简洁的替代方法,如列表理解。

R0912: Too many branches (%s/%s) Used when a function or method has too many branches, making it hard to follow.

R0912: Too many branch (%s/%s) 当一个函数或方法有太多的分支,使其难以遵循时使用。

R0913: Too many arguments (%s/%s) Used when a function or method takes too many arguments.

R0913:参数过多 (%s/%s) 当函数或方法采用过多参数时使用。

R0914: Too many local variables (%s/%s) Used when a function or method has too many local variables.

R0914:局部变量过多 (%s/%s) 当函数或方法有过多局部变量时使用。

R0903: Too few public methods (%s/%s) Used when class has too few public methods, so be sure it's really worth it.

R0903:公共方法太少 (%s/%s) 当类的公共方法太少时使用,所以确保它真的值得。

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.

W0212:访问客户端类的受保护成员 %s 当受保护成员(即名称以下划线开头的类成员)是在类或定义它的类的后代之外访问时使用。

W0312: Found indentation with %ss instead of %ss Used when there are some mixed tabs and spaces in a module.

W0312:发现缩进用 %ss 而不是 %ss 当模块中有一些混合制表符和空格时使用。

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.

C0111:缺少文档字符串当模块、函数、类或方法没有文档字符串时使用。一些特殊的方法,比如__init__不一定需要文档字符串。

C0103: Invalid name "%s" (should match %s) Used when the name doesn't match the regular expression associated to its type (constant, variable, class...).

C0103:无效名称“%s”(应匹配 %s)当名称与其类型(常量、变量、类...)关联的正则表达式不匹配时使用。

回答by gurney alex

-E will only flag what pylint think is an error (i.e. no warnings, no conventions...)

-E 只会标记 pylint 认为是错误的内容(即没有警告,没有约定......)

回答by Hyman Kelly

To persistently disable warnings and conventions:

要永久禁用警告和约定:

  1. Create a ~/.pylintrcfile by running pylint --generate-rcfile > ~/.pylintrc
  2. Edit ~/.pylintrc
  3. Uncomment disable=and change that line to disable=W,C
  1. ~/.pylintrc通过运行创建一个文件pylint --generate-rcfile > ~/.pylintrc
  2. 编辑 ~/.pylintrc
  3. 取消注释disable=并将该行更改为disable=W,C

回答by jumpy

using grep like :

使用 grep 像:

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