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

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


当前回答

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

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

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

有几种方法可以禁用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采取过于宽松的方法。