如何在 Eclipse/PyDev 中抑制“未使用的变量”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13944234/
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-09-19 19:38:19  来源:igfitidea点击:

How to suppress "unused variable" warnings in Eclipse/PyDev

pythoneclipsevariable-assignmentpydev

提问by Jon Coombs

How to suppress "unused variable" warnings in Eclipse/PyDev

如何在 Eclipse/PyDev 中抑制“未使用的变量”警告

When I'm working with functions that return tuples, I often only need one of the values, but still want to assign to multiple variables. I would like to be able to temporarily turn this warning off so I can zero in on more serious issues. Then, I can turn it back on when doing more of a final check.

当我使用返回元组的函数时,我通常只需要其中一个值,但仍想分配给多个变量。我希望能够暂时关闭此警告,以便我可以将更严重的问题归零。然后,我可以在进行更多最终检查时将其重新打开。

If you're wondering why I would do this deliberately, it's just for readability. Say a function returns a tuple of tuples, several parts of my code might work with the third value like this:

如果您想知道为什么我会故意这样做,那只是为了可读性。假设一个函数返回一个元组元组,我的代码的几个部分可能会像这样使用第三个值:

label, content = myfunc()[2]

At times, I may only be interested in the 'content' piece, but I find this...

有时,我可能只对“内容”部分感兴趣,但我发现这...

tmp, content = myfunc()[2]

...to be more parallel (and thus more readable) than this:

...比这更平行(因此更具可读性):

content = myfunc()[2][1]

If there's a better way to do this simply without assigning to a disposable unused variable, feel free to provide that as an answer.

如果有更好的方法来做到这一点,而无需分配给一次性未使用的变量,请随时提供它作为答案。

>>> myfunc()[2]
('lab', 'val')
>>> , v = myfunc()[2]
SyntaxError: invalid syntax
>>> tmp, v = myfunc()[2]
>>> 

回答by abarnert

If you don't need the value of a variable, assign it to the special variable _.

如果不需要变量的值,请将其分配给特殊变量_

As far as Python is concerned, there is actually nothing special about _; it's just another legal identifier name like any other.

就 Python 而言,实际上并没有什么特别之处_;它只是另一个合法的标识符名称。

However, for most "lint"-style tools (hopefully including PyDev)—and, more importantly, human readers—it has the special meaning that "I don't need this variable, I'm only putting something here because the API/syntax/whatever requires it". Which means they won't warn you for not using it.

然而,对于大多数“lint”风格的工具(希望包括 PyDev)——更重要的是,对于人类读者——它具有特殊的意义,“我不需要这个变量,我只是把一些东西放在这里,因为 API/语法/任何需要它的东西”。这意味着他们不会警告您不要使用它。

So:

所以:

_, content = myfunc()[2]

And yes, you are right that this is often more readable than myfunc()[2][1]. Not only that, but it helps you catch a few more errors—if myfunc()[2]doesn't have exactly two members, the tuple assignment will throw, but the [1]won't.

是的,您是对的,这通常比myfunc()[2][1]. 不仅如此,它还可以帮助您捕获更多错误——如果myfunc()[2]没有正好有两个成员,元组赋值会抛出,但[1]不会。

Very, very rarely, this is not a good idea because the value is something that you want to be garbage collected as soon as possible, and binding it to _instead of just not binding it at all (e.g., via [2][1]) delays that.

很少,这不是一个好主意,因为该值是您希望尽快被垃圾收集的东西,并且将其绑定到_而不是根本不绑定它(例如, via [2][1])会延迟它。

More seriously, this does conflict with a different idiom that also makes special use of _: Code that uses gettextfor internationalization typically does:

更严重的是,这确实与另一种习惯用法冲突,该习惯用法也特别使用_gettext用于国际化的代码通常会:

import gettext
_ = gettext.gettext

Or, equivalently:

或者,等效地:

from gettext import gettext as _

Obviously you can't use _as both the gettext shortcut and the meaningless identifier. (You couldactually get away with it, because the gettextmeaning is bound at module-global level, and the meaningless identifier should only be used inside function bodies… but still, it's a very bad idea to try, because at some point you will end up using the gettext_in a function after you've assigned a local value that shadows it.) Nothing's forcing you to use _in either case—but if you use anything else, you are likely to confuse readers (and possibly the same linting tool you're looking to pacify in the first place). So, you have to decide which one is more important to you in any given project. (And usually, if you're using gettext, that's going to be the more important one.)

显然,您不能同时_用作 gettext 快捷方式和无意义的标识符。(你可以真正摆脱它,因为gettext意思是在模块全球层面的约束,以及毫无意义的标识应该只是内部函数体中使用...但是,它仍然是一个非常糟糕的主意去尝试,因为在某些时候你会结束gettext_在分配了一个隐藏它的局部值之后使用in 函数。)没有什么会强迫您_在任何一种情况下使用- 但是如果您使用其他任何东西,您可能会混淆读者(并且可能与您使用的相同的 linting 工具)重新寻求安抚摆在首位)。因此,您必须决定在任何给定项目中哪个对您更重要。(通常,如果您使用的是gettext,那'

If you're repeatedly calling myfuncand disposing of some of the values, you might want to consider writing a wrapper function:

如果您反复调用myfunc和处理某些值,您可能需要考虑编写一个包装函数:

def mywrapperfunc():
    _, content = myfunc()[2]
    return content

Then your code can just do:

然后你的代码可以做:

content = mywrapperfunc()

This has a number of advantages:

这有许多优点:

  • It's obviously easier to read than anything that requires you to remember that you want the second half of a tuple which is in index 2 of the sequence that's returned by myfunc.
  • It gives you a place to put a nice name (hopefully nicer than mywrapperfunc) and/or comments/docstrings, in case it isn't trivial.
  • It means that if you later change myfuncso the value you want is now in index 3 instead of 2, and the second member of a 3-element tuple instead of a 2-element tuple, you only need to change mywrapperfuncinstead of 20 different lines of code.
  • It also means that if you later want to use a conflicting _idiom (e.g., to i18n your code with gettext), you only need to change it in one place.
  • 显然,这比任何要求您记住您想要元组的后半部分的内容都容易阅读,该元组位于由myfunc.
  • 它为您提供了一个放置一个好名字(希望比 更好mywrapperfunc)和/或评论/文档字符串的地方,以防它不是微不足道的。
  • 这意味着如果您稍后更改myfunc所以您想要的值现在在索引 3 而不是 2 中,并且 3 元素元组的第二个成员而不是 2 元素元组,您只需要更改mywrapperfunc而不是 20 行代码。
  • 这也意味着,如果您以后想要使用冲突的_习惯用法(例如,将您的代码与gettext),您只需在一个地方更改它。

One side note: In the interactive interpreter, _doeshave a special meaning: it's bound to the result of the last interactive command. But that doesn't mean you can't use _in the interactive interpreter. (In fact, it's even better there, because anything you stash there is immediately overwritten, so the very rare GC problem doesn't come up.)

边注:在交互式解释器中,_确实有一个特殊的含义:它绑定到最后一个交互式命令的结果。但这并不意味着您不能_在交互式解释器中使用。(事实上​​,那里更好,因为你藏在那里的任何东西都会立即被覆盖,所以不会出现非常罕见的 GC 问题。)

回答by joshua

Add the comment #@UnusedVariableto the end of the line.

将注释添加#@UnusedVariable到行尾。

Every warning in PyDev has a similar deactivation comment. Use Quick Fix to discover them (place the cursor in the warning and press Ctrl+1), or refer to these lines from the PyDev source code:

PyDev 中的每个警告都有类似的停用注释。使用 Quick Fix 来发现它们(将光标放在警告中并按 Ctrl+1),或参考PyDev 源代码中的这些行:

public static final String MSG_TO_IGNORE_TYPE_UNUSED_IMPORT = "@UnusedImport";
public static final String MSG_TO_IGNORE_TYPE_UNUSED_WILD_IMPORT = "@UnusedWildImport";
public static final String MSG_TO_IGNORE_TYPE_UNUSED_VARIABLE = "@UnusedVariable";
public static final String MSG_TO_IGNORE_TYPE_UNDEFINED_VARIABLE = "@UndefinedVariable";
public static final String MSG_TO_IGNORE_TYPE_DUPLICATED_SIGNATURE = "@DuplicatedSignature";
public static final String MSG_TO_IGNORE_TYPE_REIMPORT = "@Reimport";
public static final String MSG_TO_IGNORE_TYPE_UNRESOLVED_IMPORT = "@UnresolvedImport";
public static final String MSG_TO_IGNORE_TYPE_NO_SELF = "@NoSelf";
public static final String MSG_TO_IGNORE_TYPE_UNDEFINED_IMPORT_VARIABLE = "@UndefinedVariable";
public static final String MSG_TO_IGNORE_TYPE_UNUSED_PARAMETER = "@UnusedVariable";
public static final String MSG_TO_IGNORE_TYPE_NO_EFFECT_STMT = "@NoEffect";
public static final String MSG_TO_IGNORE_TYPE_INDENTATION_PROBLEM = "@IndentOk";
public static final String MSG_TO_IGNORE_TYPE_ASSIGNMENT_TO_BUILT_IN_SYMBOL = "@ReservedAssignment";
public static final String MSG_TO_IGNORE_TYPE_PEP8 = "@IgnorePep8";
public static final String MSG_TO_IGNORE_TYPE_ARGUMENTS_MISATCH = "@ArgumentMismatch";

回答by Mark Chackerian

Preferences -> PyDev -> Editor -> Code Analysis , "Unused" tab

首选项 -> PyDev -> 编辑器 -> 代码分析,“未使用”选项卡

Find the setting the for

找到设置

Don't report unused variable if name starts with: (comma separated)

如果名称以:(逗号分隔)开头,则不要报告未使用的变量

and then use one of the prefixes on this list, or add another prefix.

然后使用此列表中的一个前缀,或添加另一个前缀。

For example, if you have a throwaway variable "tmp" in the following code:

例如,如果您在以下代码中有一个一次性变量“tmp”:

tmp, content = myfunc()[2]

and you have '_' on your list of prefixes to ignore, then convert "tmp" to "_tmp" like this:

并且您的前缀列表中有“_”要忽略,然后将“tmp”转换为“_tmp”,如下所示:

_tmp, content = myfunc()[2]

Your error will go away. I think this is a more readable solution than just using '_', as suggested by @abarnert, and it also avoids the complications of conflicting with translation.

你的错误会消失。我认为这是一个比@abarnert 建议的仅使用“_”更具可读性的解决方案,它还避免了与翻译冲突的复杂性。

回答by sid16rgt

I run into this some times when I use a feature similar to the one you describe when a tuple is returned. You can globally set warning levels for the code analysis of PyDev in the Preferences -> PyDev -> Editor -> Code Analysis section. In Code Analysis in the Unused tab, there is an option to set the warning level for "Unused variable" as well as other occurrences.

当我使用类似于您在返回元组时描述的功能时,有时会遇到这种情况。您可以在 Preferences -> PyDev -> Editor -> Code Analysis 部分为 PyDev 的代码分析全局设置警告级别。在“未使用”选项卡的“代码分析”中,有一个选项可以为“未使用的变量”以及其他事件设置警告级别。

回答by Philippe

Or, you can suppress a given warning in Eclipse in anylanguage (including Python) using an extra plugin. http://suppresswarnings.drupalgardens.com

或者,您可以使用额外的插件以任何语言(包括 Python)抑制 Eclipse 中的给定警告。 http://suppresswarnings.drupalgardens.com

Also avaible on the marketplace:

也可在市场上获得:

https://marketplace.eclipse.org/content/marker-manager

https://marketplace.eclipse.org/content/marker-manager