如何禁用pylint no-self-use warning?

jul*_*lka 13 python pylint python-3.x

我在Python3中编码并使用pylint来保持我的代码干净.

我想定义类似接口类的东西,所以我可以以简洁明了的方式添加更多功能,但是,pylint会妨碍这个目标.

这是一个示例方法:

def on_enter(self, dummy_game, dummy_player): #pylint disable=no-self-use
    """Defines effects when entering area."""
    return None
Run Code Online (Sandbox Code Playgroud)

这是pylint输出:

R: 70, 4: Method could be a function (no-self-use)
Run Code Online (Sandbox Code Playgroud)

问题是:

  1. 如何禁止警告(注意#pylint评论)?要么
  2. 我怎么告诉pylint这只是一个界面(请注意dummy_gamedummy_player

编辑:输出pylint --version:

pylint 1.2.1, 
astroid 1.1.1, common 0.61.0
Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1]
Run Code Online (Sandbox Code Playgroud)

jul*_*lka 17

事实证明我缺乏冒号:


pylint disable=no-self-use
它本来应该使用的时候
pylint: disable=no-self-use

好吧,至少从现在开始我将永远拥有最新的(以及为python3构建的)pylint :)


mu *_*u 無 15

你现在忽略了这一点

def on_enter(self, dummy_game, dummy_player): #pylint disable=no-self-use
    ...
Run Code Online (Sandbox Code Playgroud)

相反

# pylint: disable=R0201
def on_enter(self, dummy_game, dummy_player): 
    ...
Run Code Online (Sandbox Code Playgroud)

在您的文件中添加评论,如下所示

# pylint: disable=R0201
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到文档中每个警告/错误的短代码助记符:

no-self-use (R0201):

方法可以是函数当方法不使用其绑定实例时使用,因此可以写为函数.

如果整个文件只包含接口的代码,您可以将它放在顶部:

# pylint: disable=R0201
class SomeInterface(object):
    ...
    ...
Run Code Online (Sandbox Code Playgroud)

如果您还有其他代码,并且只想为接口类禁用此功能,则可以再次启用检查

# pylint: disable=R0201
class SomeInterface(object):
    ...
    ...

# pylint: enable=R0201

class AnotherClass(object):
    ...
    ...
Run Code Online (Sandbox Code Playgroud)

  • 上帝,我没有冒号:我使用了 `pylint disable=no-self-use`,而它应该是 `pylint: disable=no-self-use` (2认同)