简体   繁体   English

Pycharm - 禁用'局部变量'xxx'可能在赋值之前被引用'

[英]Pycharm - Disable 'Local variable 'xxx' might be referenced before assignment'

In pycharm, I would like to disable the following inspection warn: "Local variable 'xxx' might be referenced before assignment" but I can't find it in settings/inspections.在 pycharm 中,我想禁用以下检查警告:“局部变量 'xxx' 可能在赋值之前被引用”,但我在设置/检查中找不到它。

PS: This is not a duplicate, as I understand this warn. PS:这不是重复的,因为我理解这个警告。 I am just asking how to disable it in pycharm.我只是问如何在 pycharm 中禁用它。

Update: Please find below an example of what I mean更新:请在下面找到我的意思的例子

cond = True
def add1(x):
    return x+1
if cond:
    a = 1
if cond:
    b = add1(a) # the warn is on the 'a'

Solution:解决方案:

"Unbound local variable" inspection. “未绑定局部变量”检​​查。 (cf. Lomtrur answer below) (参见下面的 Lomtrur 回答)

You can disable it locally by putting the following comment on the line preceding the warning:您可以通过在警告之前的行中添加以下注释来在本地禁用它:

# noinspection PyUnboundLocalVariable

It will only apply to that instance.它仅适用于该实例。

If you put that bit of code right before the function or method declaration, it will suppress the message for the entire function or method.如果将那段代码放在函数或方法声明之前,它将抑制整个函数或方法的消息。

In your case在你的情况下

if cond:
    # noinspection PyUnboundLocalVariable
    b = add1(a)

Place the cursor immediately after a .立即将光标后a It should have a colored background or be underlined to show that this is where the warning is.它应该具有彩色背景或带有下划线以表明这是警告所在的位置。 Then press Alt+Enter to open the context menu.然后按Alt+Enter打开上下文菜单。 This should show you what the inspection is and also give the option to disable it.这应该向您显示检查是什么,并提供禁用它的选项。 (PyCharm 2018.2.5 Professional Edition) (PyCharm 2018.2.5 专业版)

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

相关问题 局部变量可能在赋值前被引用 - Local Variable might ne referenced before assignment 在赋值之前可能会引用局部变量 - Python - Local variable might be referenced before assignment - Python 局部变量“result”可能在赋值前被引用 - Local variable 'result' might be referenced before assignment 局部变量“put”可能在赋值之前被引用 - Local variable 'put' might be referenced before assignment 在 Python 中赋值之前可能会引用局部变量 - local variable might be referenced before assignment in Python 分配前可能会引用局部变量 - Local variable might be referenced before assignment 如何重写这段代码,这样 PyCharm 就不会警告 Local variable might be referenced before assignment? - How to rewrite this code, such that PyCharm doesn't warn Local variable might be referenced before assignment? Pylint 没有捕捉到“局部变量 'xyz' 可能在赋值之前被引用”但 PyCharm 使用 python 2.7 突出显示相同 - Pylint is not catching "Local variable 'xyz' might be referenced before assignment " but PyCharm is highlighing the same, using python 2.7 PyCharm:在分配之前可能会在finally块中引用变量吗? - PyCharm: Variable in finally block might be referenced before assignment? 强制 While 循环使局部变量可能在赋值前被引用 - Mandatory While Loop Gives Local Variable Might Be Referenced Before Assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM