简体   繁体   English

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

[英]How to suppress “unused variable” warnings in Eclipse/PyDev

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]
>>> 

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 _ ; 就Python而言, _实际上并没有什么特别之处; it's just another legal identifier name like any other. 它只是另一个合法的标识符名称。

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". 然而,对于大多数“lint”式工具(希望包括PyDev) - 更重要的是,人类读者 - 它具有特殊意义“我不需要这个变量,我只是在这里放一些因为API /语法/任何需要它“。 Which means they won't warn you for not using it. 这意味着他们不会警告你不使用它。

So: 所以:

_, content = myfunc()[2]

And yes, you are right that this is often more readable than myfunc()[2][1] . 是的,你是对的,这通常比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]则不会。

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 (eg, via [2][1] ) delays that. 非常非常罕见,这不是一个好主意,因为值是你希望尽快被垃圾收集的东西,并将它绑定到_而不是根本不绑定它(例如,通过[2][1] )推迟。

More seriously, this does conflict with a different idiom that also makes special use of _ : Code that uses gettext for 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. 显然你不能使用_作为gettext快捷方式和无意义的标识符。 (You could actually get away with it, because the gettext meaning 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). (其实你可以逃脱它,因为gettext意思是在模块全球层面的约束,以及毫无意义的标识应该只是内部函数体中使用...但是,它仍然是一个非常糟糕的主意去尝试,因为在某些时候你会在你分配了一个阴影它的局部值之后,最终在函数中使用gettext _ 。在任何一种情况下都没有强迫你使用_但是如果你使用其他任何东西,你可能会混淆读者(可能是相同的)你想要首先安抚的linting工具)。 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 ,那将是更重要的一个。)

If you're repeatedly calling myfunc and 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 . 它显然比任何需要你记住你想要的元组的后半部分更容易阅读,这个元组位于myfunc返回的序列的索引2中。
  • It gives you a place to put a nice name (hopefully nicer than mywrapperfunc ) and/or comments/docstrings, in case it isn't trivial. 它为您提供了一个放置一个好名字(希望比mywrapperfunc更好)和/或评论/文档字符串的地方,以防它不是微不足道的。
  • It means that if you later change myfunc so 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 mywrapperfunc instead of 20 different lines of code. 这意味着如果你以后更改myfunc所以你想要的值现在是索引3而不是2,而3元素元组的第二个成员而不是2元素元组,你只需要改变mywrapperfunc而不是20个不同代码行。
  • It also means that if you later want to use a conflicting _ idiom (eg, to i18n your code with gettext ), you only need to change it in one place. 这也意味着如果您以后想要使用冲突的_惯用法(例如,使用gettext将代码用于i18n),您只需要在一个地方更改它。

One side note: In the interactive interpreter, _ does have 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问题不会出现。)

Add the comment #@UnusedVariable to the end of the line. 将注释#@UnusedVariable添加到行尾。

Every warning in PyDev has a similar deactivation comment. PyDev中的每个警告都有类似的停用注释。 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 : 使用“快速修复”来发现它们(将光标放在警告中并按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";

Preferences -> PyDev -> Editor -> Code Analysis , "Unused" tab 首选项 - > PyDev - >编辑器 - >代码分析,“未使用”选项卡

Find the setting the for 找到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所建议的那样,它也避免了与翻译冲突的复杂性。

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. 您可以在Preferences - > PyDev - > Editor - > Code Analysis部分中为PyDev的代码分析全局设置警告级别。 In Code Analysis in the Unused tab, there is an option to set the warning level for "Unused variable" as well as other occurrences. 在“未使用”选项卡的“代码分析”中,可以选择为“未使用的变量”以及其他事件设置警告级别。

Or, you can suppress a given warning in Eclipse in any language (including Python) using an extra plugin. 或者,您可以使用额外的插件以任何语言(包括Python)抑制Eclipse中的给定警告。 http://suppresswarnings.drupalgardens.com http://suppresswarnings.drupalgardens.com

Also avaible on the marketplace: 也适用于市场:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM