Skip to content

Commit b6d7fc9

Browse files
committed
Add tests for nonlocal checks
1 parent de97e5d commit b6d7fc9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tests/functional/u/used/used_before_assignment_nonlocal.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,30 @@ def inner():
106106
print(args)
107107
inner()
108108
print(args)
109+
110+
111+
def nonlocal_in_outer_frame_fail():
112+
"""nonlocal declared in outer frame, bad usage and assignation in inner frame"""
113+
num = 1
114+
def outer():
115+
nonlocal num
116+
def inner():
117+
print(num) # [used-before-assignment]
118+
num = 2
119+
inner()
120+
outer()
121+
122+
123+
def nonlocal_in_outer_frame_ok(callback, condition_a, condition_b):
124+
"""nonlocal declared in outer frame, usage and definition in different frames"""
125+
def outer():
126+
nonlocal callback
127+
if condition_a:
128+
def inner():
129+
callback() # should not emit possibly-used-before-assignment
130+
inner()
131+
else:
132+
if condition_b:
133+
def callback():
134+
pass
135+
outer()

tests/functional/u/used/used_before_assignment_nonlocal.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ used-before-assignment:33:44:33:53:test_fail4:Using variable 'undefined' before
66
used-before-assignment:39:18:39:28:test_fail5:Using variable 'undefined1' before assignment:HIGH
77
used-before-assignment:90:10:90:18:type_annotation_never_gets_value_despite_nonlocal:Using variable 'some_num' before assignment:HIGH
88
used-before-assignment:96:14:96:18:inner_function_lacks_access_to_outer_args.inner:Using variable 'args' before assignment:HIGH
9+
used-before-assignment:117:18:117:21:nonlocal_in_outer_frame_fail.outer.inner:Using variable 'num' before assignment:HIGH

0 commit comments

Comments
 (0)