How to set coc-pyright to not warn for unused self parameter in a method?

Viewed 1360

I'm using the coc-pyright extension of CoC in neovim.

There are cases where we have an instance method in a class that doesn't need to access self variables. For example, I may have some variables in __init__ method which do not need to be accessed anywhere else in the class. As a toy example, consider,

class Example:
    def __init__(self, some_var):
        self.another_var = self.process_var(some_var)

    def process_var(self, some_var):
        return some_var*2

Now, here pyright warns me that self is not accessed. Is there a way to set Pyright to not give a warning in such cases?

P.S. The variable being passed is an instance-specific variable. So self would be required to make it an instance variable, right?

1 Answers

So you're passing some_var to the method instead of using it as an instance variable, meaning you don't actually need self. You are using process_var as a static method, as it is being passed all of the data it needs. You can use the static method decorator and remove self like so:

class Example:
    def __init__(self, some_var):
        self.another_var = self.process_var(some_var)

    @staticmethod
    def process_var(some_var):
        return some_var*2

If you were to instead use the instance variable it would look like this

class Example:
    def __init__(self, some_var):
        self.some_var = some_var
        self.another_var = self.process_var()

    def process_var(self):
        return self.some_var*2

The static method approach is much cleaner here as by using instance variables you need to be aware of what the current state of some_var is before you call process_var, otherwise the results will vary.

You can set reportUnusedVariable to false in your pyrightconfig.json file, but I wouldn't recommend this as pyright is giving you valuable information about how to restructure your code.

Related