1

My IDE is warning me against a possible variable reference before assignment. But I'm having trouble spotting it. I'm ready to turn in a bug report, but maybe you can see what I'm not seeing.

The variable in question is a named check_dialog and the reference before assignment warning happens at the end of the following code snippet in the line I marked for you:

    if dialog:
        validate = None
        string = False

        if dialog == Prompt.YES | Prompt.NO:
            check_dialog = lambda c: chr(c) in 'yn'
        elif dialog == Prompt.CONTINUE | Prompt.QUIT:
            check_dialog = lambda c: chr(c) in 'cq'
        elif dialog == Prompt.YES | Prompt.NO | Prompt.QUIT:
            check_dialog = lambda c: chr(c) in 'ynq'
        else:
            raise ValueError('Invalid dialog argument.')

    answer = None
    while not answer:
        self.addstr(0, 1, prompt)

        if string:
            curses.curs_set(True)
            curses.echo()
            answer = self.getstr(1, 3)
            curses.noecho()
            curses.curs_set(False)
        elif dialog:
            while not check_dialog(answer):  # warning here!
                answer = self.getch()
        else:
            answer = self.getch()
Alexandre Bell
  • 3,141
  • 3
  • 30
  • 43
  • Probably because it sees that it is not assigned in the `else` above, so it isn't there in every case. However, since that else raises a ValueError, it makes no practical difference. Try something like `check_dialog = None` in that `else` to silence your IDE's warning, but it is safe to ignore. – Two-Bit Alchemist Mar 19 '15 at 14:58
  • Yeah. Tried that before. It spits a new warning that check_variable is not being used and also maintains the previous warning. Hehe :) Only way to silence it is to define check_dialog at the top before the first if statement. It's probably a bug – Alexandre Bell Mar 19 '15 at 15:03
  • OK. I was just about to edit my comment saying you might need to define it before the first if, but it's for the same reason (that if has no else). – Two-Bit Alchemist Mar 19 '15 at 15:04
  • Oh, you mean the first if statement. Well, I suppose... But it still wouldn't make sense to declare it there. – Alexandre Bell Mar 19 '15 at 15:06

1 Answers1

1

Your IDE is not "thinking" about every possible value of your variables (in most cases, this would be impossible) and instead is using heuristics to prevent common mistakes. In this case, it has noticed that check_dialog is defined within an if condition, but not in every case. Yet it is used below this condition. That might be an UnboundLocalError!

As programmers, we can reason this out and see that the code paths it has noticed are protected. The else case raises a ValueError which will not be caught, and the usage is protected by (el)if dialog in both cases, so this will not be a problem.

It is not a bug in your IDE, because it is doing what it is supposed to. If it really bothers you and you can't otherwise silence the warning, you can unnecessarily define something like check_dialog = None over the top of the first if dialog, and it will shut up. However, it is also not a bug with your program/code, which as reasoned above will not cause an UnboundLocalError. This is safe to ignore, and because of how your IDE probably works a bug report would just be closed.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82