shadowed variables not noticed after list comprehension

Bug #1373143 reported by Brian Warner
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pyflakes
Confirmed
Undecided
Unassigned

Bug Description

With pyflakes-0.8.1 and python-2.7.8, the following test program gets the expected complaint:

import foo
foo()
def bar():
    a = 0
    #a = [1 for foo in []]
    for foo in []:
        pass
    del a

pyflakes t.py
t.py:6: import 'foo' from line 1 shadowed by loop variable

But if you uncomment the list comprehension, the pyflakes complaints go away.

I think the presence of the list comprehension should actually *increase* the number of complaints: "foo" is being shadowed in both the list comprehension and the for-loop.

Revision history for this message
Brian Warner (warner) wrote :
description: updated
Revision history for this message
Ian Cordasco (icordasc) wrote :

You mention this happens on 2.7. The 2.x branch has an issue with list comprehension bindings being leaked to the outer scope. So you can do this on 2.7

[i for i in range(10)]
print(i + 1)

And you'll see 10.

I agree that uncommenting the list comprehension shouldn't cause the error to stop but I suspect what I mentioned is what's causing it. It appears, to pyflakes, that the variable is being defined locally in some way that does not appear to shadow the global binding.

I think I agree that there should also be (ideally) two errors/warnings: 1 for using `foo` in the list comprehension and 1 for using it afterwards.

Changed in pyflakes:
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.