如何禁用pylint无自用警告?

时间:2022-04-01 16:46:42

I'm coding in Python3 and using pylint to keep my code clean.

我用Python3编写代码,并且使用pylint保持代码整洁。

I want to define something like interface class, so I could add more functionality in a clean and concise way, however, pylint gets in the way of this goal.

我想定义一些东西,比如接口类,这样我就可以以一种干净而简洁的方式添加更多的功能,然而,pylint阻碍了这个目标的实现。

Here's a sample method:

下面是一个示例方法:

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

Here's pylint output:

这是pylint输出:

R: 70, 4: Method could be a function (no-self-use)

The question is:

问题是:

  1. How do I suppress warning (notice the #pylint comment)? or
  2. 如何抑制警告(注意#pylint注释)?或
  3. How do I tell pylint that this is merely an interface (notice the dummy_game and dummy_player
  4. 如何告诉pylint这只是一个接口(注意dummy_game和dummy_player)

EDIT: Output of pylint --version:

编辑:pylint的输出—版本:

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]

2 个解决方案

#1


11  

You are currently ignoring this as

你现在忽略了这个as

def on_enter(self, dummy_game, dummy_player): #pylint disable=no-self-use
    ...

Instead do

而不是做

# pylint: disable=R0201
def on_enter(self, dummy_game, dummy_player): 
    ...

Add a comment to your file like below

向你的文件添加一个注释,如下所示

# pylint: disable=R0201

You can find the short codes mnemonics to for each of the warnings/errors on documentation here:

您可以在这里找到文档上的每个警告/错误的简短代码助记符:

no-self-use (R0201):

no-self-use(R0201):

Method could be a function Used when a method doesn’t use its bound instance, and so could be written as a function.

方法可以是当一个方法不使用它的绑定实例时使用的函数,因此可以作为一个函数来编写。

In case the whole file contains code for the interface only, you can put this at the top:

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

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

In case you have other code as well, and want to disable this for the interface class only, you can enable the check again like

如果您还有其他代码,并希望仅为接口类禁用此功能,您可以再次启用该检查。

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

# pylint: enable=R0201

class AnotherClass(object):
    ...
    ...

#2


13  

Turns out I was lacking colon :

原来我没有结肠:

I used
pylint disable=no-self-use
when it should have been
pylint: disable=no-self-use

我使用pylint disable=no-self-use,当它应该是pylint: disable=no-self-use

Well, at least I will always have the latest (and the one built for python3) pylint from now on :)

好吧,至少我将永远拥有最新的(也是为python3构建的)pylint:

#1


11  

You are currently ignoring this as

你现在忽略了这个as

def on_enter(self, dummy_game, dummy_player): #pylint disable=no-self-use
    ...

Instead do

而不是做

# pylint: disable=R0201
def on_enter(self, dummy_game, dummy_player): 
    ...

Add a comment to your file like below

向你的文件添加一个注释,如下所示

# pylint: disable=R0201

You can find the short codes mnemonics to for each of the warnings/errors on documentation here:

您可以在这里找到文档上的每个警告/错误的简短代码助记符:

no-self-use (R0201):

no-self-use(R0201):

Method could be a function Used when a method doesn’t use its bound instance, and so could be written as a function.

方法可以是当一个方法不使用它的绑定实例时使用的函数,因此可以作为一个函数来编写。

In case the whole file contains code for the interface only, you can put this at the top:

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

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

In case you have other code as well, and want to disable this for the interface class only, you can enable the check again like

如果您还有其他代码,并希望仅为接口类禁用此功能,您可以再次启用该检查。

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

# pylint: enable=R0201

class AnotherClass(object):
    ...
    ...

#2


13  

Turns out I was lacking colon :

原来我没有结肠:

I used
pylint disable=no-self-use
when it should have been
pylint: disable=no-self-use

我使用pylint disable=no-self-use,当它应该是pylint: disable=no-self-use

Well, at least I will always have the latest (and the one built for python3) pylint from now on :)

好吧,至少我将永远拥有最新的(也是为python3构建的)pylint: