30

Is there a way to declare a variable as unused in PyCharm, or in Python in general, so you can explicitly tell the compiler not to give a warning about it?

I am not talking about the convention of naming unused variables for the programmer (often named "_" or "__"), but an option to explicitly mark a variable as unused for the compiler, for example in a loop. I also don't just want to disable inspections in general.

I've heard that you can do this in PyDev by beginning the variable name with "unused", and I thought this might exist in PyCharm as well, but couldn't find it yet.

Raimund Krämer
  • 1,255
  • 1
  • 11
  • 29
  • You can suppress an inspection for a statement. Python in general doesn't warn. But for PyLint you can [disable warnings](http://stackoverflow.com/questions/4341746/how-do-i-disable-a-pylint-warning) by using a comment. – Peter Wood Sep 16 '15 at 08:23
  • here a similar question about function arguments: https://stackoverflow.com/questions/10025680/how-can-i-denote-unused-function-arguments – hiro protagonist Jan 05 '20 at 07:58

5 Answers5

30

You can easily and least intrusively ignore pycharm unused local warnings (only) for unused function parameters by prefixing them with underscores.

E.g.

In the following code, pycharm will not warn about the unused parameter _bar

def foo(_bar):
    print("nothing here")
Anubis
  • 6,995
  • 14
  • 56
  • 87
25

You can disable this inspection either for a single statement like:

# noinspection PyUnusedLocal
unused_thing = something()

or for a whole function (or class) by placing the comment above the function (or class):

# noinspection PyUnusedLocal
def foo():
    unused_thing = something()

For some reason this particular inspection cannot be switched off via the inspections context menu... maybe worth a pycharm ticket.

sebastian
  • 9,526
  • 26
  • 54
10

I've noticed that using a single underscore for the throwaway variable name seems to bypass this check. I'm using PyCharm 2016.1.3.

for _ in range(3):
    pass
Justin
  • 6,611
  • 3
  • 36
  • 57
  • Good for pylint too. – Rafe Mar 04 '17 at 20:10
  • PyCharm seems to recognise double underscore `__` now too, e.g. `for _, __ in my_dict.items()` – c z Oct 23 '17 at 13:44
  • 1
    A single underscore is sometimes used as an alias for gettext, in which case this trick cannot be used. But in the settings for the "Unused local" inspection, you can configure PyCharm to ignore any variables starting with an underscore. – Cito Apr 25 '20 at 15:41
4

Another way, similar to UNUSED in C++ (here), which works if you want to hide the warning on a specific function parameter but keeps the warning enabled for the rest of the function:

# noinspection PyUnusedLocal
def UNUSED(*args, **kwargs):
    pass

def my_function(alpha, beta, gamma):
    UNUSED(gamma)
    return alpha + beta
c z
  • 7,726
  • 3
  • 46
  • 59
0

Just to extend to sebastian's answer, if you use a decorator with the function. You need to place the

# noinspection PyUnusedLocal

above the decorator if you place it between the decorator and the function name it will not work.

# noinspection PyUnusedLocal
@torch.no_grad()
def step(self, closure=None):
    """Performs a single optimization step.

    Arguments:
        closure (callable, optional): A closure that reevaluates the model
            and returns the loss.
    """
    pass
KoKlA
  • 898
  • 2
  • 11
  • 15