Python 是否可以使用 pylint 忽略单个特定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28829236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to ignore one single specific line with pylint?
提问by The Unfun Cat
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?
这实际上改变了我的 python 日志记录设置,但 pylint 认为这是一个未使用的导入。我一般不想删除unused-import
警告,所以可以忽略这一特定行吗?
I wouldn't mind having a .pylintrc
for this project so answers changing a config file will be accepted.
我不介意.pylintrc
为这个项目提供一个,所以更改配置文件的答案将被接受。
Otherwise, something like this will also be appreciated:
否则,这样的事情也会受到赞赏:
import config.logging_settings # pylint: disable-this-line-in-some-way
采纳答案by jomo
Pylint message control is documented in the Pylint manual:
Pylint 消息控制记录在Pylint 手册中:
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
是否可以在本地禁用特定消息?
是的,此功能已在 Pylint 0.11 中添加。这可以通过
# pylint: disable=some-message,another-one
在所需的块级别或所需代码行的末尾添加来完成
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 wikithat documents all pylint messages and their codes.
有一个 wiki记录了所有 pylint 消息及其代码。
回答by Basic
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 Ctrl1while the cursor is over the warning will auto-suggest @UnusedImport
此外,根据您的 IDE(如果您正在使用),可能有一个选项可以添加正确的忽略规则(例如,在 Eclipse 中Ctrl1,当光标悬停在光标上方时,警告将自动建议@UnusedImport
回答by The Unfun Cat
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:
正如 sthenault 友好指出的那样,您可以而且应该使用更易读的形式:
import config.logging_settings # pylint: disable=unused-import
回答by loxaxs
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:
检查https://github.com/PyCQA/pylint/tree/master/pylint/checkers 中的文件。我还没有找到比 Ctrl+F-ing 这些文件或使用 Github 搜索功能更好的从消息中获取错误名称的方法:
If the message is "No name ... in module ...", use the search:
如果消息是“No name ... in module ...”,请使用搜索:
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:
Github 会告诉你:
"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