我试图在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中的警告和错误。使用哪一个与您希望如何全局或局部地应用禁用有关——这是一个重要的设计决策。

多种方法

在一个或多个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采取过于宽松的方法。

其他回答

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

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

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

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

pylint --disable=C,R

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。

对不起,我偏离了最初的问题,关于海报的一般偏好,这将更好地解决一个全局配置文件。 但是,正如在许多流行的答案中一样,我倾向于在我的代码中看到什么可以触发警告,并最终通知贡献者。 我回复@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: disable = line-too-long, too-many-lines, no-name-in-module, import-error, multiple-imports, pointless-string-statement, wrong-import-order

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

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

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

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