45

The say module brings string interpolation to Python, like this:

import say

def f(a):
    return say.fmt("The value of 'a' is {a}")

However, PyLint complains that the variable 'a' is never used. This is a problem because my code uses say.fmt extensively. How can I silence this warning?

Eleno
  • 2,864
  • 3
  • 33
  • 39

5 Answers5

67

Yes, you can silence pylint warnings.

Here is one way:

import say

def f(a):
    # pylint: disable=unused-argument
    return say.fmt("The value of 'a' is {a}")

Alternatively, you can create a config file and add these lines to it:

[MESSAGES CONTROL]
disable=unused-argument

Reference:

Josiah
  • 2,666
  • 5
  • 30
  • 40
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
24

One approach to silencing that message is to name or prefix the argument with dummy or _, as in:

import say

def f(_a):
    return say.fmt("The value of 'a' is {_a}")

See here for more info: https://stackoverflow.com/a/10107410/1080804

ecoe
  • 4,994
  • 7
  • 54
  • 72
9

There is disable-possibly-unused-variable now (since pylint 2.0 was released on 2018-07-15), which one could ignore in files importing your say module:

New possibly-unused-variable check added.

This is similar to unused-variable, the only difference is that it is emitted when we detect a locals() call in the scope of the unused variable. The locals() call could potentially use the said variable, by consuming all values that are present up to the point of the call. This new check allows to disable this error when the user intentionally uses locals() to consume everything.

For instance, the following code will now trigger this new error:

def func():
    some_value = some_call()
    return locals()

The rationale for this check explicitly includes your use case, though it's noted that it's not a perfect solution:

It would be great to have a separate check for unused variables if locals() is used in the same scope:

def example_no_locals():
  value = 42  # pylint: disable=unused-variable

def exmaple_defined_before():
  value = 42  # pylint: disable=possibly-unused-variable
  print(locals())

def exmaple_defined_after():
  print(locals())
  value = 42  # pylint: disable=unused-variable

The benefit of this is that one can disable probably-unused-variable for a file (that has a lot of string formatting in it, or the config code example in #641) or the whole project without also loosing checks for unused-variable.

mtd
  • 2,224
  • 19
  • 21
5

I use this pattern:

def foo(a, b, c):
  _ = (a, b)  # unused: use these later
  return c + 1

It's less verbose than a waiver, I don't need to munge the keyword argument names, and I have a handy reminder to get back to using those variables later.

Jonathan Mayer
  • 132
  • 1
  • 5
  • This should be voted higher. I was having the issue with monkey-patched methods, so I couldn't just change the argument names. – Opus4210 Dec 02 '22 at 07:30
  • IMO this is the better than all the other current solutions because: (1.) it allows you to specifically disable the pylint warning for a,b and not c (in the future you may have d,e,f which you do not want to ignore) (2.) the solution is still one line of code (like `# pylint: disable=unused-argument`) – Trevor Boyd Smith Jan 24 '23 at 13:53
0

We can resolve this issue by using "" before our arguments EXAMPLE-: def foo(a,b): was giving pylint issue i.e. 'a' (unused-argument) and 'b' (unused-argument), so I have added "" before my arguments and it got resolved like def foo(_a,_b):