Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silence PyLint warning about unused variables for string interpolation

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?

like image 648
Eleno Avatar asked Feb 17 '16 22:02

Eleno


People also ask

How do you stop Pylint warnings?

This may be done by adding # pylint: disable=some-message,another-one at the desired block level or at the end of the desired line of code.

How do you handle unused variables in Python?

To suppress the warning, one can simply name the variable with an underscore ('_') alone. Python treats it as an unused variable and ignores it without giving the warning message.


2 Answers

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:

  • https://pylint.readthedocs.io/en/latest/faq.html#is-it-possible-to-locally-disable-a-particular-message
  • https://pylint.readthedocs.io/en/latest/user_guide/run.html#command-line-options
like image 105
Robᵩ Avatar answered Sep 20 '22 09:09

Robᵩ


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

like image 43
ecoe Avatar answered Sep 21 '22 09:09

ecoe