Skip to content

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

@pyflakes-bot

Description

@pyflakes-bot

Original report by fawio on Launchpad:


Hi,
As far as I know, bug 872503 (#142) 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions