Silence PyLint 关于未使用的字符串插值变量的警告
Silence PyLint warning about unused variables for string interpolation
say
模块给Python带来字符串插值,像这样:
import say
def f(a):
return say.fmt("The value of 'a' is {a}")
但是,PyLint 抱怨从未使用过变量 'a'。这是一个问题,因为我的代码广泛使用 say.fmt
。我怎样才能消除这个警告?
是的,您可以使 pylint 警告静音。
这是一种方法:
import say
def f(a):
# pylint: disable=unused-argument
return say.fmt("The value of 'a' is {a}")
或者,您可以创建一个配置文件并向其中添加以下行:
[MESSAGES CONTROL]
disable=unused-argument
参考:
消除该消息的一种方法是使用 dummy
或 _
为参数命名或添加前缀,如:
import say
def f(_a):
return say.fmt("The value of 'a' is {_a}")
查看此处了解更多信息:
现在有 disable-possibly-unused-variable
(自 pylint 2.0 was released on 2018-07-15 起),在导入您的 say
模块的文件中可以忽略:
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()
此检查的基本原理 explicitly includes your use case,但注意到它不是一个完美的解决方案:
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.
say
模块给Python带来字符串插值,像这样:
import say
def f(a):
return say.fmt("The value of 'a' is {a}")
但是,PyLint 抱怨从未使用过变量 'a'。这是一个问题,因为我的代码广泛使用 say.fmt
。我怎样才能消除这个警告?
是的,您可以使 pylint 警告静音。
这是一种方法:
import say
def f(a):
# pylint: disable=unused-argument
return say.fmt("The value of 'a' is {a}")
或者,您可以创建一个配置文件并向其中添加以下行:
[MESSAGES CONTROL]
disable=unused-argument
参考:
消除该消息的一种方法是使用 dummy
或 _
为参数命名或添加前缀,如:
import say
def f(_a):
return say.fmt("The value of 'a' is {_a}")
查看此处了解更多信息:
现在有 disable-possibly-unused-variable
(自 pylint 2.0 was released on 2018-07-15 起),在导入您的 say
模块的文件中可以忽略:
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()
此检查的基本原理 explicitly includes your use case,但注意到它不是一个完美的解决方案:
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.