Pycharm - Disable 'Local variable 'xxx' might be referenced before assignment'
Asked Answered
D

2

7

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.

PS: This is not a duplicate, as I understand this warn. I am just asking how to disable it in 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)

Disobedient answered 14/3, 2019 at 5:29 Comment(4)
are you using a global variable xxx? what causes this warning?Mansur
no, it's a local variable. In a main(), if cond : var =fct1() and later fct2(var) shows this warn. But I don't care, that's my coding style.Disobedient
What happens if you click after the a and press Alt+Enter? That should tell you which inspection you want to disable, or does that not work? What is your PyCharm version?Earpiercing
It was the "Unbound local variable" inspection. Thanks for this method Lomtrur.Disobedient
E
4

Place the cursor immediately after 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. This should show you what the inspection is and also give the option to disable it. (PyCharm 2018.2.5 Professional Edition)

Earpiercing answered 14/3, 2019 at 6:3 Comment(3)
Great. Solution is "Unbound local variable" inspection. Thanks for your method, I also learned to fish.Disobedient
Also, this method, as well as the code analysis method, are both described in the documentation section on suppressing inspections. If you're not aware of the code analysis method, I'd check it out as it can be powerful too.Imaginable
Unfortunately this doesn't work consistently on the newer versions.Sufficient
H
13

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)
Hustle answered 6/8, 2020 at 16:10 Comment(0)
E
4

Place the cursor immediately after 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. This should show you what the inspection is and also give the option to disable it. (PyCharm 2018.2.5 Professional Edition)

Earpiercing answered 14/3, 2019 at 6:3 Comment(3)
Great. Solution is "Unbound local variable" inspection. Thanks for your method, I also learned to fish.Disobedient
Also, this method, as well as the code analysis method, are both described in the documentation section on suppressing inspections. If you're not aware of the code analysis method, I'd check it out as it can be powerful too.Imaginable
Unfortunately this doesn't work consistently on the newer versions.Sufficient

© 2022 - 2024 — McMap. All rights reserved.