inconsistent behavior with locals(), comprehensions and the unused local variable warning

Bug #1325966 reported by Fabio Pesari
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pyflakes
Confirmed
Low
Unassigned

Bug Description

Hi,
As far as I know, bug #872503 was meant to fix the issue of code using locals() not being recognized as using local variables and indeed the code

# No warning
def test():
    a = 5
    return locals()

does not raise any warnings, but I've noticed that using locals() in the context of a comprehension (except a list comprehension) will incorrectly make pyflakes assume that the locals are not being used.

Using code as an example,

# No warning
def test():
    a = 5
    return [i for i in locals().values()]

does not raise a warning but the following generator expression:

# local variable 'a' is assigned to but never used
def test():
    a = 5
    return (i for i in locals().values())

set comprehension:

# local variable 'a' is assigned to but never used
def test():
    a = 5
    return {i for i in locals().values()}

and dict comprehension:

# local variable 'a' is assigned to but never used
def test():
    a = 5
    return {k: v for k, v in locals().items()}

all raise the unused local variable warning, which is pretty inconsistent with the previous behavior.

This is not a hugely critical bug but it's annoying and it could imply some bugs in the comprehensions elsewhere. Thanks for reading.

Florent (florent.x)
Changed in pyflakes:
importance: Undecided → Low
status: New → Confirmed
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.