Skip to content

No error for nonlocal with a missing variable #21495

@eltoder

Description

@eltoder

Summary

The nonlocal statement requires that the variable is a local variable in one of the parent scopes. ruff does not check this. For example:

def no_local_var():
    def setter(xx):
        nonlocal x
        x = xx
        print(x)

    return setter

(playground link: https://play.ruff.rs/f6ca3be7-208f-4ade-b68e-148d3546af6b)

This passes all checks:

$ ruff check --isolated t2.py
All checks passed!

However this fails with a SyntaxError when run:

$ python t2.py
  File "/home/eltoder/dev/scratch/t2.py", line 3
    nonlocal x
    ^^^^^^^^^^
SyntaxError: no binding for nonlocal 'x' found

Version

0.14.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsking for support or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions