Skip to content

Pyflakes doesn't properly handle del inside function definition #428

@asmeurer

Description

@asmeurer
a = 1

def test():
    a = 2
    del a
    print(a)

test()

The code produces UnboundLocalError: local variable 'a' referenced before assignment, but pyflakes shows now errors.

The reason this code raises an exception is that local variables are determined statically at compile time, not at runtime, so the a = 2 line forces a to be function local inside of test. The del a does not make it global again (it is impossible to "make a local variable global again").

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