Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zenlyj committed Nov 9, 2024
1 parent a067bdc commit db56fc9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,9 @@ def _is_builtin(self, name: str) -> bool:
def _has_nonlocal_in_enclosing_frame(
self, node: nodes.Name, uncertain_definitions: list[nodes.NodeNG]
) -> bool:
"""Check if there is a nonlocal declaration in the nearest frame that encloses both usage and definitions."""
"""Check if there is a nonlocal declaration in the nearest frame that encloses
both usage and definitions.
"""
defining_frames = {definition.frame() for definition in uncertain_definitions}
frame = node.frame()
is_enclosing_frame = False
Expand Down
32 changes: 31 additions & 1 deletion tests/functional/u/used/used_before_assignment_nonlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def inner():


def nonlocal_in_outer_frame_ok(callback, condition_a, condition_b):
"""Nonlocal declared in outer frame, usage and definition in different frames."""
"""Nonlocal declared in outer frame, usage and definition in different frames,
both enclosed in outer frame.
"""
def outer():
nonlocal callback
if condition_a:
Expand All @@ -133,3 +135,31 @@ def inner():
def callback():
pass
outer()


def nonlocal_in_distant_outer_frame_fail(callback, condition_a, condition_b):
"""Nonlocal declared in outer frame, both usage and definition immediately enclosed
in intermediate frame.
"""
def outer():
nonlocal callback
def intermediate():
if condition_a:
def inner():
callback() # [possibly-used-before-assignment]
inner()
else:
if condition_b:
def callback():
pass
intermediate()
outer()


def nonlocal_after_bad_usage_fail():
"""Nonlocal declared after used-before-assignment."""
num = 1
def inner():
num = num + 1 # [used-before-assignment]
nonlocal num
inner()
2 changes: 2 additions & 0 deletions tests/functional/u/used/used_before_assignment_nonlocal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ used-before-assignment:39:18:39:28:test_fail5:Using variable 'undefined1' before
used-before-assignment:90:10:90:18:type_annotation_never_gets_value_despite_nonlocal:Using variable 'some_num' before assignment:HIGH
used-before-assignment:96:14:96:18:inner_function_lacks_access_to_outer_args.inner:Using variable 'args' before assignment:HIGH
used-before-assignment:117:18:117:21:nonlocal_in_outer_frame_fail.outer.inner:Using variable 'num' before assignment:HIGH
possibly-used-before-assignment:149:20:149:28:nonlocal_in_distant_outer_frame_fail.outer.intermediate.inner:Possibly using variable 'callback' before assignment:CONTROL_FLOW
used-before-assignment:163:14:163:17:nonlocal_after_bad_usage_fail.inner:Using variable 'num' before assignment:HIGH

0 comments on commit db56fc9

Please sign in to comment.