Skip to content

F821 "Undefined name" when variable is deleted in another branch #6242

@Louis-dM

Description

@Louis-dM

Suppose I del a variable in an Exception branch:

def my_fn(a: str) -> str:
    try:
        print(a)
    except Exception:
        del a
        raise
    else:
        return a

a must always exist in the else branch, but Ruff claims it's undefined:

~$ ruff foobar.py --isolated
foobar.py:8:16: F821 Undefined name `a`
Found 1 error.

(line 8 char 16 is the location of the a in the return statement)

This is on version 0.0.281:

~$ ruff --version
ruff 0.0.281

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions