我试图在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错误,还是我做错了什么?有办法解决这个问题吗?

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


当前回答

对不起,我偏离了最初的问题,关于海报的一般偏好,这将更好地解决一个全局配置文件。 但是,正如在许多流行的答案中一样,我倾向于在我的代码中看到什么可以触发警告,并最终通知贡献者。 我回复@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——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。

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

例如,

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

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

我在使用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控制台)。

这是一个常见问题:

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消息的完整列表。文档还提供了如何使用该特性的很好的示例。

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

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

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

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

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

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