我试图在Pylint 0.21.1中禁用警告C0321(“单行中有多个语句”——我经常将具有短单行结果的if语句放在同一行上)(如果有问题:astng 0.20.1, common 0.50.3,和Python 2.6.6 (r266:84292, 2010年9月15日,16:22:56))。

我尝试在Pylint配置文件中添加disable=C0321,但Pylint坚持报告它。这一行的变体(如disable=0321或disable=C321)被标记为错误,因此Pylint能够正确地识别该选项。它只是忽略它。

这是一个Pylint错误,还是我做错了什么?有办法解决这个问题吗?

我真的很想摆脱这些噪音。


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在section [MESSAGES CONTROL]中应该有disable= line/s。

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

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

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

这就创建了标准。Rc配置文件。在记事本中打开它,在[消息控制]下,取消注释 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类实例的属性。运行Pylint时,使用参数——disable-ids=…(至少没有配置文件),这个字典最初是空的,在Pylint (Pylint .utils. messageshandlermixin .check_message_id())中引发KeyError异常。

在Eclipse中,你可以在Pylint控制台中看到这个错误消息(windows*→显示视图→控制台,在控制台图标旁边的控制台选项中选择Pylint控制台)。

还可以使用以下命令:

pylint --disable=C0321  test.py

我的Pylint版本是0.25.1。

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

# pylint: disable=C0321

到那个街区。

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

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

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

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

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

如果Pylint输出“本地禁用”消息,你可以通过在上面的例子中首先包含禁用local -disabled来摆脱它们。

有几种方法可以禁用Pylint中的警告和错误。使用哪一个与您希望如何全局或局部地应用禁用有关——这是一个重要的设计决策。

多种方法

在一个或多个pylintrc文件中。

这不仅仅涉及到~/。pylintrc文件(在$HOME目录下),如Chris Morgan所述。Pylint将搜索rc文件,优先级为“更接近”的文件:

当前工作目录中的pylintrc文件;或 如果当前工作目录在Python模块中(即它包含__init__.py文件),则向上搜索Python模块的层次结构,直到找到pylintrc文件;或 由环境变量PYLINTRC命名的文件;或 如果你的主目录不是/root: ~ / .pylintrc;或 ~ / config / pylintrc;或 /etc/pylintrc

注意,这些文件中的大多数都被命名为pylintrc——只有~中的文件有一个前导点。

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

[MESSAGES CONTROL]
disable=locally-disabled

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

注意,禁用一行使用# pylint语法,而禁用这一行使用# pragma pylint语法。这些很容易混淆,特别是在复制和粘贴时。

把它们放在一起

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

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.


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

这是一个常见问题:

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

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

数字编号:E1101, E1102等。 符号消息:无成员、未定义变量等。 一组检查的名称。你可以用pylint——list-groups来获取它们。 支票类别:C、R、W等。 所有人的支票。

请参阅文档(或在终端中运行pylint——list-msgs)以获得pylint消息的完整列表。文档还提供了如何使用该特性的很好的示例。

为了帮助别人,如果你使用Visual Studio Code,它期望文件是UTF-8编码。为了生成文件,我在PowerShell中运行pylint——generate-rcfile | out-file -encoding utf8 .pylintrc。

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

所以,解决这个问题的另一种方法是,理解为什么lint消息在那里,而不是在一行上放置一个以上的语句。

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

你只需要添加一行来禁用你想禁用的东西。

例如,

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

将此添加到模块的最开始。

编辑“C:\Users\Your 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"
    ],
}

根据Pylint文档,最简单的是使用这个图表:

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

所以你可以用:

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

对不起,我偏离了最初的问题,关于海报的一般偏好,这将更好地解决一个全局配置文件。 但是,正如在许多流行的答案中一样,我倾向于在我的代码中看到什么可以触发警告,并最终通知贡献者。 我回复@imolit的评论需要保持简短,以下是一些细节。

对于多语句消息,最好在块或模块级别禁用它,就像这样

# pylint: disable=multiple-statements

我的用例现在是unittest setup()中的attribute-defined-outside-init,我选择了行范围消息禁用,使用消息代码来避免行太长问题。

class ParserTest(unittest.TestCase):
   def setUp(self):
       self.parser = create_parser()  # pylint: disable=W0201

可以在本地使用类似的命令找到对应关系

$ pylint --list-msgs | grep 'outside-init'
:attribute-defined-outside-init (W0201): *Attribute %r defined outside __init__*

当然,您也可以类似地从代码中检索符号名称。

我的pylint一直忽略.pylintrc中的禁用列表。最后,我意识到我在执行:

pylint --disable=all --enable=F,E,W

这是覆盖禁用列表在我的。pylintrc。

只显示致命、错误、警告的正确命令是:

pylint --disable=C,R