Silence PyLint warning about unused variables for string interpolation
Question:
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?
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:
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
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.
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.
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?
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:
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
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.
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.