问题:是否可以忽略带有pylint的单个特定行?

标头中包含以下行:

import config.logging_settings

这实际上改变了我的python日志记录设置,但是pylint认为这是未使用的导入。我一般不希望删除unused-import警告,因此可以忽略这一行吗?

我不介意.pylintrc为此项目准备一个,因此更改配置文件的答案将被接受。

否则,这样的事情也将不胜感激:

import config.logging_settings # pylint: disable-this-line-in-some-way

I have the following line in my header:

import config.logging_settings

This actually changes my python logging settings, but pylint thinks it is an unused import. I do not want to remove unused-import warnings in general so is it possible to just ignore this one specific line?

I wouldn’t mind having a .pylintrc for this project so answers changing a config file will be accepted.

Otherwise, something like this will also be appreciated:

import config.logging_settings # pylint: disable-this-line-in-some-way

回答 0

Pylint信息控件记录在Pylint手册中

是否可以在本地禁用特定消息?

是的,此功能已在Pylint 0.11中添加。这可以通过
# pylint: disable=some-message,another-one
在所需的块级别或所需的代码行的末尾添加来完成

您可以使用消息代码或符号名。

例如

def test():
    # Disable all the no-member violations in this function
    # pylint: disable=no-member
    ...
global VAR # pylint: disable=global-statement

该手册还有更多示例

一个Wiki,记录所有pylint消息及其代码。

Pylint message control is documented in the Pylint manual:

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

You can use the message code or the symbolic names.

For example

def test():
    # Disable all the no-member violations in this function
    # pylint: disable=no-member
    ...
global VAR # pylint: disable=global-statement

The manual also has further examples.

There is a wiki that documents all pylint messages and their codes.


回答 1

import config.logging_settings # pylint: disable=W0611

这很简单,并且专门针对该行。

正如sthenault所指出的那样,您可以并且应该使用更具可读性的形式:

import config.logging_settings # pylint: disable=unused-import
import config.logging_settings # pylint: disable=W0611

That was simple and is specific for that line.

As sthenault kindly pointed out, you can and should use the more readable form:

import config.logging_settings # pylint: disable=unused-import

回答 2

我相信您正在寻找的是…

import config.logging_settings  # @UnusedImport

注意注释前的双倍空格,以免出现其他格式警告。

另外,根据您的IDE(如果使用的是IDE),可能有一个选项可以添加正确的忽略规则(例如,Ctrl1在光标悬停时按eclipse键,警告将自动建议@UnusedImport

I believe what you’re looking for is…

import config.logging_settings  # @UnusedImport

Note the double space before the comment to avoid hitting other formatting warnings.

Also, depending on your IDE (if you’re using one), there’s probably an option to add the correct ignore rule (eg in eclipse pressing Ctrl1 while the cursor is over the warning will auto-suggest @UnusedImport


回答 3

https://github.com/PyCQA/pylint/tree/master/pylint/checkers中检出文件。我没有找到比Ctrl + F-ing这些文件或使用Github搜索功能更好的从消息中获取错误名称的方法:

如果消息是“模块…中没有名称…”,请使用搜索:

No name %r in module %r repo:PyCQA/pylint/tree/master path:/pylint/checkers

或者,为了获得更少的结果:

"No name %r in module %r" repo:PyCQA/pylint/tree/master path:/pylint/checkers

Github将向您展示:

"E0611": (
    "No name %r in module %r",
    "no-name-in-module",
    "Used when a name cannot be found in a module.",

然后,您可以执行以下操作:

from collections import Sequence # pylint: disable=no-name-in-module

Checkout the files in https://github.com/PyCQA/pylint/tree/master/pylint/checkers. I haven’t found a better way to obtain the error name from a message than either Ctrl+F-ing those files or using the Github search feature:

If the message is “No name … in module …”, use the search:

No name %r in module %r repo:PyCQA/pylint/tree/master path:/pylint/checkers

Or, to get less results:

"No name %r in module %r" repo:PyCQA/pylint/tree/master path:/pylint/checkers

Github will show you:

"E0611": (
    "No name %r in module %r",
    "no-name-in-module",
    "Used when a name cannot be found in a module.",

You can then do:

from collections import Sequence # pylint: disable=no-name-in-module

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。