Python 关于字符串插值的未使用变量的静音 PyLint 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35469253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:29:08  来源:igfitidea点击:

Silence PyLint warning about unused variables for string interpolation

pythonpylintunused-variables

提问by Elena

The saymodule brings string interpolation to Python, like this:

say模块为 Python 带来了字符串插值,如下所示:

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.fmtextensively. How can I silence this warning?

但是,PyLint 抱怨从未使用过变量“a”。这是一个问题,因为我的代码say.fmt广泛使用。我怎样才能让这个警告静音?

采纳答案by Rob?

Yes, you can silence pylint warnings.

是的,您可以使 pylint 警告静音。

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:

参考:

回答by ecoe

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

使该消息静音的一种方法是使用dummyor命名或前缀参数_,如下所示:

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

请参阅此处了解更多信息:https: //stackoverflow.com/a/10107410/1080804

回答by mtd

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

disable-possibly-unused-variable现在(因为pylint的2.0发布于2018年7月15日),其中一个可以在文件中导入您忽略say模块:

New possibly-unused-variablecheck 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()

添加了新的可能未使用的变量检查。

这类似于未使用的变量,唯一的区别是当我们在未使用的变量范围内检测到 locals() 调用时会发出它。locals() 调用可能会使用上述变量,方法是使用调用之前出现的所有值。当用户有意使用 locals() 来消费所有内容时,这项新检查允许禁用此错误。

例如,以下代码现在将触发此新错误:

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.

如果在同一范围内使用 locals() ,最好对未使用的变量进行单独检查:

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

这样做的好处是可以为文件(其中包含大量字符串格式,或 #641 中的配置代码示例)或整个项目禁用可能未使用的变量,而不会丢失对未使用变量的检查。