我试图在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: disable=C0321

到那个街区。

其他回答

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

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

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

所以你可以用:

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

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

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

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

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

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

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

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。

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

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

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