Comment 1 for bug 1461208

Revision history for this message
Phil Frost (bitglue) wrote :

Ah, yeah. So there was another change, which I can't find now, which addressed this situation (going from memory here):

foo = "foo"
if False:
  del foo
print foo

This would complain about the final 'print foo' referencing an undefined foo, but as you can see, this code runs just fine.

The underlying issue here is that pyflakes doesn't have any notion of of conditional branches. Whenever it sees an 'if', 'while', 'try/except', or similar, it just ignores it, and proceeds as if the body of the branch were not in any branch at all. Another case illustrates why:

if False:
  foo = "foo"
print foo

You'll notice pyflakes won't complain about this, even though it clearly won't work. Of course usually the conditional expression is something more complicated, and pyflakes can't actually evaluate it since that would require that it is a python interpreter. So it just makes a stupid assumption.

So, I'll have to think more about how to fix this. I think it's a situation where all cases can't be accurately covered without making some changes to the underlying assumptions that pyflakes makes. Probably it needs to build a graph of the possible code paths and consider the state of the namespace along each possible path through the graph.

I'm calling this medium priority for now since it's hard to fix without making at least one of these cases wrong, and hopefully just a minor annoyance. If that characterization seems wrong, please post some more code examples and we can look at reverting to the previous behavior.