如何禁用Pylint警告?

2024-03-29 08:33:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图禁用警告C0321(“一行有多个语句”--我经常在同一行上放置带有短单行结果的if语句),在Pylint 0.21.1中(如果重要的话:astng 0.20.1,common 0.50.3,Python 2.6.6(r266:8429220010年9月15日,16:22:56))。

我试过在Pylint配置文件中添加disable=C0321,但Pylint坚持无论如何都要报告它。该行上的变量(如disable=0321disable=C321)被标记为错误,因此Pylint确实正确地识别了该选项,只是忽略了它。

这是Pylint虫,还是我做错了什么?有办法解决这个问题吗?我真的很想去掉一些噪音。


Tags: 警告if配置文件报告语句commonpylint行上
3条回答

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=行在一个[MESSAGES CONTROL]段中。

从Pylint v.0.25.3开始,您可以使用符号名来禁用警告instead of having to remember all those code numbers。E、 g.:

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

这种样式比隐藏的错误代码更有指导意义,而且更实用,因为较新版本的Pylint只输出符号名,而不输出错误代码。

符号名和代码之间的对应关系可以找到here

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

如果pylint输出“Locally disabling”消息,您可以通过将disablelocally-disabled包含在前面的示例中来除去它们。

我在使用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 message codes


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

相关问题 更多 >