Python 如何禁用 Pylint 警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4341746/
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:19:17  来源:igfitidea点击:

How do I disable a Pylint warning?

pythonpylint

提问by Head Geek

I'm trying to disable warning C0321 ("more than one statement on a single line" -- I often put ifstatements with short single-line results on the same line), in Pylint 0.21.1 (if it matters: astng 0.20.1, common 0.50.3, Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)).

我正在尝试在 Pylint 0.21.1 中禁用警告 C0321(“在一行上有多个语句”——我经常将if带有短单行结果的语句放在同一行)(如果重要:astng 0.20。 1,通用 0.50.3,Python 2.6.6(r266:84292,2010 年 9 月 15 日,16:22:56))。

I've tried adding disable=C0321in the Pylint configuration file, but Pylint insists on reporting it anyway. Variations on that line (like disable=0321or disable=C321) are flagged as errors, so Pylint doesrecognize the option properly, it's just ignoring it.

我已经尝试添加disable=C0321Pylint 配置文件,但 Pylint 仍然坚持报告它。该行上的变体(如disable=0321disable=C321)被标记为错误,因此 Pylint确实正确识别了该选项,只是忽略了它。

Is this a Pylint bug, or am I doing something wrong? Is there any way around this? I'd really like to get rid of some of this noise.

这是 Pylint 错误,还是我做错了什么?有没有办法解决?我真的很想摆脱一些这种噪音。

采纳答案by Chris Morgan

pylint --generate-rcfileshows it like this:

pylint --generate-rcfile显示如下:

[MESSAGES CONTROL]

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time.
#enable=

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
#disable=

So it looks like your ~/.pylintrcshould have the disable=line/s in it inside a section [MESSAGES CONTROL].

所以看起来你~/.pylintrc应该disable=在 section 中包含line/s [MESSAGES CONTROL]

回答by Remi

I had this problem using Eclipseand solved it as follows:

我在使用Eclipse 时遇到了这个问题,并按如下方式解决了它:

in the pylint folder (e.g. C:\Python26\Lib\site-packages\pylint), hold shift, right-click and choose to open the windows command in that folder. Type:

在 pylint 文件夹(例如C:\Python26\Lib\site-packages\pylint)中,按住 shift,右键单击并选择在该文件夹中打开 windows 命令。类型:

lint.py --generate-rcfile > standard.rc

This creates the standard.rcconfiguration file. Open it in notepad and under [MESSAGES CONTROL], uncomment disable=and add the message ID's you want to disable, e.g.:

这将创建standard.rc配置文件。在记事本和下打开它[MESSAGES CONTROL],取消注释 disable=并添加要禁用的消息 ID,例如:

disable=W0511, C0321

Save the file, and in Eclipse->window->preferences->PyDev->pylint, in the arguments box, type:

保存文件,在 Eclipse->window->preferences->PyDev->pylint 中,在参数框中键入:

--rcfile=C:\Python26\Lib\site-packages\pylint\standard.rc

Now it should work ...

现在它应该工作......



You can also add a commentat the top of your code that will be interpreted by pylint:

您还可以在代码顶部添加由 pylint 解释的注释:

# pylint: disable=C0321


link to all pylint message codes

链接到所有pylint 消息代码



Adding e.g. --disable-ids=C0321in the arguments box does not work. All available pylint messages are stored in the dictionary _messages, an attribute of an instance of the pylint.utils.MessagesHandlerMixInclass. When running pylint with the argument --disable-ids=...(at least without a config file), this dictionary is initially empty, raising a KeyError exception within pylint (pylint.utils.MessagesHandlerMixIn.check_message_id(). In Eclipse, you can see this error-message in the Pylint Console (windows - show view - Console, select Pylint console from the console options besides the console icon.)

--disable-ids=C0321在参数框中添加例如不起作用。所有可用的 pylint 消息都存储在字典中_messages,这是pylint.utils.MessagesHandlerMixIn类实例的一个属性。当使用参数运行 pylint 时--disable-ids=...(至少没有配置文件),这个字典最初是空的,在 pylint ( pylint.utils.MessagesHandlerMixIn.check_message_id(). ,从控制台图标旁边的控制台选项中选择 Pylint 控制台。)

回答by aboo

You can also use the following command:

您还可以使用以下命令:

pylint --disable=C0321  test.py

My pylint version is 0.25.1.

我的 pylint 版本是 0.25.1。

回答by thakis

To disable a warning locally in a block, add

要在块中本地禁用警告,请添加

# pylint: disable=C0321

to that block.

到那个街区。

回答by imolit

Starting from Pylint v. 0.25.3, you can use the symbolic names for disabling warnings instead of having to remember all those code numbers. E.g.:

从 Pylint v. 0.25.3 开始,您可以使用符号名称来禁用警告,而不必记住所有这些代码。例如:

# pylint: disable=locally-disabled, multiple-statements, fixme, line-too-long

This style is more instructive than cryptic error codes, and also more practical since newer versions of Pylint only output the symbolic name, not the error code.

这种风格比神秘的错误代码更有指导意义,也更实用,因为较新版本的 Pylint 只输出符号名称,而不是错误代码。

The correspondence between symbolic names and codes can be found here.

符号名称和代码之间的对应关系可以在这里找到。

A disable comment can be inserted on its own line, applying the disable to everything that comes after in the same block. Alternatively, it can be inserted at the end of the line for which it is meant to apply.

禁用注释可以单独插入一行,将禁用应用于同一块中后面的所有内容。或者,它可以插入到要应用的行的末尾。

If pylint outputs "Locally disabling" messages, you can get rid of them by including the disable locally-disabledfirstas in the example above.

如果 pylint 输出 " Locally disabling" 消息,您可以通过在上面的示例中locally-disabled首先包含 disable 来摆脱它们。

回答by Chris Johnson

There are several ways to disable warnings & errors from Pylint. Which one to use has to do with how globally or locally you want to apply the disablement -- an important design decision.

有多种方法可以禁用来自 Pylint 的警告和错误。使用哪个与您希望在全局或本地应用禁用的方式有关——这是一个重要的设计决策。

Multiple Approaches

多种方法

  1. In one or more pylintrcfiles.
  1. 在一个或多个pylintrc文件中。

This involves more than the ~/.pylintrcfile (in your $HOME directory) as described by Chris Morgan. Pylint will search for rc files, with a precedence that values "closer" files more highly:

这不仅仅涉及~/.pylintrcChris Morgan 所描述的文件(在您的 $HOME 目录中)。Pylint 将搜索 rc 文件,优先考虑“更接近”的文件更高:

  • A pylintrcfile in the current working directory; or

  • If the current working directory is in a Python module (i.e. it contains an __init__.pyfile), searching up the hierarchy of Python modules until a pylintrcfile is found; or

  • The file named by the environment variable PYLINTRC; or

  • If you have a home directory that isn't /root:

    • ~/.pylintrc; or

    • ~/.config/pylintrc; or

    • /etc/pylintrc

  • pylintrc当前工作目录中的一个文件;或者

  • 如果当前工作目录在 Python 模块中(即它包含一个__init__.py文件),则向上搜索 Python 模块的层次结构,直到pylintrc找到文件;或者

  • 由环境变量 PYLINTRC 命名的文件;或者

  • 如果您的主目录不是/root

    • ~/.pylintrc; 或者

    • ~/.config/pylintrc; 或者

    • /etc/pylintrc

Note that most of these files are named pylintrc-- only the file in ~has a leading dot.

请注意,这些文件中的大多数都已命名pylintrc——只有其中的文件~具有前导点。

To your pylintrcfile, add lines to disable specific pylint messages. For example:

在您的pylintrc文件中,添加行以禁用特定的 pylint 消息。例如:

[MESSAGES CONTROL]
disable=locally-disabled
  1. Further disables from the pylintcommand line, as described by Aboo and Cairnarvon. This looks like pylint --disable=bad-builtin. Repeat --disableto suppress additional items.

  2. Further disables from individual Python code lines, as described by Imolit. These look like some statement # pylint: disable=broad-except(extra comment on the end of the original source line) and apply only to the current line. My approach is to always put these on the end of other lines of code so they won't be confused with the block style, see below.

  3. Further disables defined for larger blocks of Python code, up to complete source files.

    • These look like # pragma pylint: disable=bad-whitespace(note the pragmakey word).

    • These apply to every line afterthe pragma. Putting a block of these at the top of a file makes the suppressions apply to the whole file. Putting the same block lower in the file makes them apply only to lines following the block. My approach is to always put these on a line of their own so they won't be confused with the single-line style, see above.

    • When a suppression should only apply within a span of code, use # pragma pylint: enable=bad-whitespace(now using enablenot disable) to stop suppressing.

  1. pylint从命令行进一步禁用,如 Aboo 和 Cairnarvon 所述。这看起来像pylint --disable=bad-builtin。重复--disable以抑制其他项目。

  2. 进一步禁用单个 Python 代码行,如 Imolit 所述。这些看起来像some statement # pylint: disable=broad-except(原始源代码行末尾的额外注释)并且仅适用于当前行。我的方法是始终将它们放在其他代码行的末尾,这样它们就不会与块样式混淆,请参见下文。

  3. 进一步禁用为更大的 Python 代码块定义,直到完整的源文件。

    • 这些看起来像# pragma pylint: disable=bad-whitespace(注意pragma关键词)。

    • 这些适用于pragma之后每一行。将其中的一个块放在文件的顶部会使抑制适用于整个文件。将相同的块放在文件的较低位置使它们仅适用于块后面的行。我的方法是始终将它们放在自己的一行上,这样它们就不会与单行样式混淆,请参见上文。

    • 当抑制应该只在一段代码内应用时,使用# pragma pylint: enable=bad-whitespace(现在使用enablenot disable)来停止抑制。

Note that disabling for a single line uses the # pylintsyntax while disabling for this line onward uses the # pragma pylintsyntax. These are easy to confuse especially when copying & pasting.

请注意,禁用单行使用# pylint语法,而禁用此行以后使用# pragma pylint语法。这些很容易混淆,尤其是在复制和粘贴时。

Putting It All Together

把它放在一起

I usually use a mix of these approaches.

我通常混合使用这些方法。

  • I use ~/.pylintrcfor absolutely global standards -- very few of these.

  • I use project-level pylintrcat different levels within Python modules when there are project-specific standards. Especially when you're taking in code from another person or team, you may find they use conventions that you don't prefer, but you don't want to rework the code. Keeping the settings at this level helps not spread those practices to other projects.

  • I use the block style pragmas at the top of single source files. I like to turn the pragmas off (stop suppressing messages) in the heat of development even for Pylint standards I don't agree with (like "too few public methods" -- I always get that warning on custom Exception classes) -- but it's helpful to see more / maybe all Pylint messages while you're developing. That way you can find the cases you want to address with single-line pragmas (see below), or just add comments for the next developer to explain why that warning is OK in this case.

  • I leave some of the block-style pragmas enabled even when the code is ready to check in. I try to use few of those, but when it makes sense for the module, it's OK to do as documentation. However I try to leave as few on as possible, preferably none.

  • I use the single-line-comment style to address especially potent errors. For example, if there's a place where it actually makes sense to do except Exception as exc, I put the # pylint: disable=broad-excepton that line instead of a more global approach because this is a strange exception and needs to be called out, basically as a form of documentation.

  • ~/.pylintrc用于绝对的全球标准——这些标准很少。

  • pylintrc当存在特定于项目的标准时,我在 Python 模块中的不同级别使用项目级别。特别是当您从其他人或团队接收代码时,您可能会发现他们使用了您不喜欢的约定,但您不想重新编写代码。将设置保持在此级别有助于不将这些实践传播到其他项目。

  • 我在单个源文件的顶部使用块样式编译指示。我喜欢在开发的热潮中关闭编译指示(停止抑制消息),即使对于我不同意的 Pylint 标准(比如“公共方法太少”——我总是在自定义异常类上收到警告)——但是在开发过程中查看更多/也许所有 Pylint 消息会很有帮助。这样你就可以找到你想用单行编译指示解决的情况(见下文),或者只是为下一个开发人员添加注释来解释为什么在这种情况下警告是可以的。

  • 即使代码准备好签入,我也会启用一些块样式的编译指示。我尝试使用其中的一些,但是当它对模块有意义时,可以作为文档来做。然而,我尽量少留下,最好没有。

  • 我使用单行注释样式来解决特别严重的错误。例如,如果有一个地方真正有意义except Exception as exc,我将# pylint: disable=broad-except放在那一行而不是更全局的方法,因为这是一个奇怪的异常并且需要被调用,基本上作为一种文档形式。



Like everything else in Python, you can act at different levels of indirection. My advice is to think about what belongs at what level so you don't end up with a too-lenient approach to Pylint.

与 Python 中的其他所有内容一样,您可以在不同的间接级别上进行操作。我的建议是考虑什么属于哪个级别,这样你就不会对 Pylint 采取过于宽松的方法。

回答by Eugene Yarmash

This is a FAQ:

这是一个常见问题

4.1 Is it possible to locally disable a particular message?

Yes, this feature has been added in Pylint 0.11. This may be done by adding
# pylint: disable=some-message,another-oneat the desired block level or at the end of the desired line of code.

4.2 Is there a way to disable a message for a particular module only?

Yes, you can disable or enable (globally disabled) messages at the module level by adding the corresponding option in a comment at the top of the file:

# pylint: disable=wildcard-import, method-hidden
# pylint: enable=too-many-lines

4.1 是否可以在本地禁用特定消息?

是的,此功能已在 Pylint 0.11 中添加。这可以通过
# pylint: disable=some-message,another-one在所需的块级别或所需代码行的末尾添加来完成。

4.2 有没有办法只为特定模块禁用消息?

是的,您可以通过在文件顶部的注释中添加相应的选项,在模块级别禁用或启用(全局禁用)消息:

# pylint: disable=wildcard-import, method-hidden
# pylint: enable=too-many-lines

You can disable messages by:

您可以通过以下方式禁用消息:

  • numerical ID: E1101, E1102etc.
  • symbolic message: no-member, undefined-variableetc.
  • the name of a group of checks. You can grab those with pylint --list-groups.
  • category of checks: C, R, W, etc.
  • all the checks with all.
  • 数字ID: E1101E1102
  • 符号信息:no-memberundefined-variable
  • 一组支票的名称。你可以用pylint --list-groups.
  • 检查类别:CRW,等。
  • 所有带有all.

See the docs(or run pylint --list-msgsin the terminal) for the full list of pylint's messages. The docs also provide a nice exampleof how to use this feature.

请参阅文档(或pylint --list-msgs在终端中运行)以获取pylint's 消息的完整列表。文档还提供了一个很好的示例来说明如何使用此功能。

回答by Elijah W. Gagne

In case this helps someone, if you're using Visual Studio Code, it expects the file to be in UTF8 encoding. To generate the file, I ran pylint --generate-rcfile | out-file -encoding utf8 .pylintrcin PowerShell.

如果这对某人有帮助,如果您使用的是 Visual Studio Code,它希望文件采用 UTF8 编码。为了生成文件,我pylint --generate-rcfile | out-file -encoding utf8 .pylintrc在 PowerShell 中运行。

回答by hrf

Python syntax does permit more than one statement on a line, separated by semicolon (;). However, limiting each line to one statement makes it easier for a human to follow a program's logic when reading through it.

Python 语法允许在一行中包含多个语句,并以分号 (;) 分隔。但是,将每一行限制为一个语句,使人们在阅读程序时更容易遵循程序的逻辑。

So, another way of solving this issue, is to understand why the lint message is there and not put more than one statement on a line.

因此,解决此问题的另一种方法是了解为什么会出现 lint 消息,而不是在一行中放置多个语句。

Yes, you may find it easier to write multiple statements per line, however, pylint is for every other reader of your code not just you.

是的,您可能会发现每行编写多个语句更容易,但是,pylint 适用于您的代码的所有其他读者,而不仅仅是您。

回答by Chetan Malhotra

You just have to add one line to disable what you want to disable. E.g.

您只需要添加一行即可禁用要禁用的内容。例如

#pylint: disable = line-too-long, too-many-lines, no-name-in-module, import-error, multiple-imports, pointless-string-statement, wrong-import-order

Add this at #1 in your module

将此添加到模块中的 #1