Erroneous 'list comprehension redefines' in Python 2.7 pyflakes 0.6.1

Bug #1112067 reported by Jayson Falkner
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Pyflakes
Fix Released
Medium
Unassigned

Bug Description

When updating from pyflakes 0.5.0 to pyflakes 0.6.1 many occurances of 'list comprehension redefines' are listed erroneously. The issue appears to be that pyflakes will complain about reusing the same loop variable name in multiple list comprehensions.

Here are two lines for example:

168:
wells = Well.objects.filter(id__in=(well.id for well in wells))

...

183:
fx_wells = [well for well in wells if well.assay_subtype == fx_subtype]

And a message such as below appears.

foo/bar.py:183:: list comprehension redefines 'well' from line 168

In this case pyflakes should be smart enough to realize that 'well' will not be incorrectly used because it will be initialized immediately with the expected value in the second list comprehension.

Related branches

Revision history for this message
Ian Cordasco (icordasc) wrote :

I couldn't reproduce this with the attached file.

Revision history for this message
James (bistromathic1) wrote :

This problem seems to occur when a dummy variable name in a generator expression, a dict comprehension, or a set comprehension is then reused in a list comprehension, for example:

a = [1, 2, 3, 4, 5, 6]

b = (i + 1 for i in a)
c = [i + 2 for i in b]

print(c)

The pyflakes output is:

test-1.py:4: list comprehension redefines 'i' from line 3

I only tried this in python 3.2.

Revision history for this message
Florent (florent.x) wrote :

actually, this should not report redefinition of variable for:
 * generator expressions
 * dict comprehensions
 * set comprehensions

Only the list comprehension leaks its variable, and only with Python 2.x.
(Though we'll keep reporting the warning for 3.x as well)

Changed in pyflakes:
status: New → Confirmed
importance: Undecided → Medium
Revision history for this message
Florent (florent.x) wrote :
Revision history for this message
Dan Kenigsberg (danken) wrote :

I'm not sure it's the very same bug, but pylakes 0.6.1 (python 2.7.3) considers the following as having a redefinition of x:

if False:
    x = 1
else:
    [x for x in [1]]

Revision history for this message
Florent (florent.x) wrote :

> I'm not sure it's the very same bug, but pylakes 0.6.1 (python 2.7.3) considers the following as having a redefinition of x:
>
> if False:
> x = 1
> else:
> [x for x in [1]]

this last one is a different bug, it should be reported separately.

Revision history for this message
Florent (florent.x) wrote :
Changed in pyflakes:
status: Confirmed → Fix Committed
Florent (florent.x)
Changed in pyflakes:
milestone: none → 0.7
Florent (florent.x)
Changed in pyflakes:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

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