julka
julka

Reputation: 1232

How to disable pylint no-self-use warning?

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

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.

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:

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. How do I tell pylint that this is merely an interface (notice the dummy_game and dummy_player

EDIT: Output of 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]

Upvotes: 18

Views: 28189

Answers (3)

Thomas Grainger
Thomas Grainger

Reputation: 2429

Just use a @staticmethod per the advice in https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/no-self-use.html

class Example:
    @staticmethod
    def on_enter(dummy_game, dummy_player):
        """Defines effects when entering area."""
        return None

Upvotes: -1

Anshul Goyal
Anshul Goyal

Reputation: 77033

You are currently ignoring this 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):

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):
    ...
    ...

Upvotes: 24

julka
julka

Reputation: 1232

Turns out I was lacking colon :

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

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

Upvotes: 32

Related Questions