如何禁用Pylint警告?

问题:如何禁用Pylint警告?

我正在尝试在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)。

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

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

I’m trying to disable warning C0321 (“more than one statement on a single line” — I often put if statements 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)).

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

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.


回答 0

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=

因此,看起来您~/.pylintrc应该disable=在section中有一行[MESSAGES CONTROL]

pylint --generate-rcfile shows it like this:

[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 ~/.pylintrc should have the disable= line/s in it inside a section [MESSAGES CONTROL].


回答 1

我在使用Eclipse时遇到了此问题,并按以下步骤解决了该问题:

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

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

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

disable=W0511, C0321

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

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

现在应该可以了…


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

# pylint: disable=C0321

链接到所有pylint消息代码


--disable-ids=C0321在参数框中添加例如无效。所有可用的pylint消息都存储在字典中_messages,字典是pylint.utils.MessagesHandlerMixIn该类实例的属性。当使用带有参数--disable-ids=...(至少没有配置文件)运行pylint时,此字典最初为空,从而在pylint(pylint.utils.MessagesHandlerMixIn.check_message_id()。中引发KeyError异常。在Eclipse中,您可以在Pylint 控制台中看到此错误消息(windows-show view-Console ,从控制台图标旁边的控制台选项中选择Pylint控制台。)

I had this problem using Eclipse and solved it as follows:

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:

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

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

disable=W0511, C0321

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

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

Now it should work …


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

# pylint: disable=C0321

link to all pylint message codes


Adding e.g. --disable-ids=C0321 in 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.MessagesHandlerMixIn class. 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.)


回答 2

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

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

这种样式比隐式错误代码更具启发性,并且也更实用,因为较新版本的Pylint仅输出符号名称,而不输出错误代码。

符号名和代码之间的对应关系可以在此处找到。

可以在其自己的行上插入禁用注释,将禁用应用于同一块之后的所有内容。或者,可以将其插入要应用的行的末尾。

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

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

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-disabled first as in the example above.


回答 3

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

# pylint: disable=C0321

到那个街区。

To disable a warning locally in a block, add

# pylint: disable=C0321

to that block.


回答 4

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

多种方法

  1. 在一个或多个pylintrc文件中。

这涉及的不只是~/.pylintrcChris Morgan描述的文件(在$ HOME目录中)。Pylint将搜索rc文件,其优先级是对“ closer”文件的重视程度更高:

  • 一个pylintrc在当前工作目录下的文件; 要么

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

  • 该文件由环境变量PYLINTRC命名;要么

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

    • ~/.pylintrc; 要么

    • ~/.config/pylintrc; 要么

    • /etc/pylintrc

请注意,其中大多数文件都已命名pylintrc-仅其中的文件~带有前导点。

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

[MESSAGES CONTROL]
disable=locally-disabled
  1. pylint如Aboo和Cairnarvon所述,从命令行进一步禁用。看起来像pylint --disable=bad-builtin。重复--disable以取消显示其他项目。

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

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

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

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

    • 当抑制仅在代码范围内适用时,请使用# pragma pylint: enable=bad-whitespace(现在使用enablenot disable)停止抑制。

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

放在一起

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

  • 我使用~/.pylintrc绝对的全球标准-其中很少。

  • pylintrc当存在特定于项目的标准时,我在Python模块中的不同级别使用项目级。特别是当您从另一个人或团队那里获取代码时,您可能会发现他们使用了您不希望使用的约定,但是您不想重做代码。将设置保持在此级别不会有助于将这些做法传播到其他项目。

  • 我在单个源文件的顶部使用块样式实用程序。即使对于我不同意的Pylint标准,我也喜欢在开发热时关闭编译指示(停止抑制消息)(例如“公共方法太少-我总是在自定义Exception类上得到警告”),但是在开发过程中查看更多(也许所有Pylint消息)很有帮助。这样,您可以找到要使用单行编译指示解决的情况(请参见下文),或仅为下一个开发人员添加注释,以解释为什么在这种情况下可以接受该警告。

  • 即使在代码准备好检入的情况下,我仍然启用了一些块样式的编译指示。我尝试使用其中的一些,但是当对模块有意义时,可以作为文档来使用。但是,我尝试保留尽可能少的内容,最好不保留任何内容。

  • 我使用单行注释样式来解决特别是潜在的错误。例如,如果有实际可行的地方,那么except Exception as exc我将其# pylint: disable=broad-except放在这行上,而不是采用更全局的方法,因为这是一个奇怪的exceptions,需要以基本上是一种文档形式的形式予以提及。


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

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.

Multiple Approaches

  1. In one or more pylintrc files.

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

  • A pylintrc file in the current working directory; or

  • If the current working directory is in a Python module (i.e. it contains an __init__.py file), searching up the hierarchy of Python modules until a pylintrc file 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

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

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

[MESSAGES CONTROL]
disable=locally-disabled
  1. Further disables from the pylint command line, as described by Aboo and Cairnarvon. This looks like pylint --disable=bad-builtin. Repeat --disable to 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 pragma key word).

    • These apply to every line after the 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 enable not disable) to stop suppressing.

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

Putting It All Together

I usually use a mix of these approaches.

  • I use ~/.pylintrc for absolutely global standards — very few of these.

  • I use project-level pylintrc at 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-except on 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.


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.


回答 5

您还可以使用以下命令:

pylint --disable=C0321  test.py

我的pylint版本是0.25.1。

You can also use the following command:

pylint --disable=C0321  test.py

My pylint version is 0.25.1.


回答 6

这是一个常见问题解答

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

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

4.2是否可以仅禁用特定模块的消息?

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

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

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

  • 数字ID: E1101E1102
  • 象征性讯息:no-memberundefined-variable等等。
  • 一组支票的名称。您可以使用抓住这些pylint --list-groups
  • 检查类别:CRW,等。
  • 用的所有支票all

请参阅文档(或pylint --list-msgs在终端中运行)以获取pylint消息的完整列表。该文档还提供了有关如何使用此功能的好示例

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-one at 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

You can disable messages by:

  • numerical ID: E1101, E1102 etc.
  • symbolic message: no-member, undefined-variable etc.
  • 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.

See the docs (or run pylint --list-msgs in the terminal) for the full list of pylint‘s messages. The docs also provide a nice example of how to use this feature.


回答 7

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

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

将其添加到模块中的#1

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


回答 8

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

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 .pylintrc in PowerShell.


回答 9

根据pylint文档,最简单的方法是使用以下图表

  • C约定相关检查
  • R重构相关检查
  • W各种警告
  • E错误,用于代码中可能的错误
  • 如果发生错误而导致pylint无法进行进一步处理,则将导致严重后果。

因此,可以使用:

pylint -j 0 --disable=I,E,R,W,C,F YOUR_FILES_LOC

As per pylint documentation, the easiest is to use this chart:

  • C convention related checks
  • R refactoring related checks
  • W various warnings
  • E errors, for probable bugs in the code
  • F fatal, if an error occurred which prevented pylint from doing further processing.

So one can use:

pylint -j 0 --disable=I,E,R,W,C,F YOUR_FILES_LOC

回答 10

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

因此,解决此问题的另一种方法是理解为什么出现了皮棉消息,并且在一行中没有放置多个语句。

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

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.

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.

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.


回答 11

您可能要尝试以下操作:

编辑“ C:\ Users \您的User \ AppData \ Roaming \ Code \ User \ settings.json”,并python.linting.pylintArgs在末尾添加以下行:

{
    "team.showWelcomeMessage": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "git.enableSmartCommit": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "files.autoSave": "onWindowChange",
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
}

Edit “C:\Users\Your User\AppData\Roaming\Code\User\settings.json” and add ‘python.linting.pylintArgs’ with its lines at the end as shown below:

{
    "team.showWelcomeMessage": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "git.enableSmartCommit": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "files.autoSave": "onWindowChange",
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
}