blob: aa0b654b7111950552d8ad9fcca160d21103a4ab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
"""Check for nonlocal and used-before-assignment"""
# pylint: disable=missing-docstring, unused-variable, no-init, too-few-public-methods
__revision__ = 0
def test_ok():
""" uses nonlocal """
cnt = 1
def wrap():
nonlocal cnt
cnt = cnt + 1
wrap()
def test_fail():
""" doesn't use nonlocal """
cnt = 1
def wrap():
cnt = cnt + 1 # [used-before-assignment]
wrap()
def test_fail2():
""" use nonlocal, but for other variable """
cnt = 1
count = 1
def wrap():
nonlocal count
cnt = cnt + 1 # [used-before-assignment]
wrap()
def test_fail3(arg: test_fail4): # [used-before-assignment]
""" Depends on `test_fail4`, in argument annotation. """
return arg
# +1: [used-before-assignment, used-before-assignment]
def test_fail4(*args: test_fail5, **kwargs: undefined):
""" Depends on `test_fail5` and `undefined` in
variable and named arguments annotations.
"""
return args, kwargs
def test_fail5()->undefined1: # [used-before-assignment]
""" Depends on `undefined1` in function return annotation. """
def undefined():
""" no op """
def undefined1():
""" no op """
def nonlocal_in_ifexp():
"""bar"""
bug2 = True
def on_click(event):
"""on_click"""
if event:
nonlocal bug2
bug2 = not bug2
on_click(True)
nonlocal_in_ifexp()
def type_annotation_only_gets_value_via_nonlocal():
"""https://github.com/PyCQA/pylint/issues/5394"""
some_num: int
def inner():
nonlocal some_num
some_num = 5
inner()
print(some_num)
def type_annotation_only_gets_value_via_nonlocal_nested():
"""Similar, with nesting"""
some_num: int
def inner():
def inner2():
nonlocal some_num
some_num = 5
inner2()
inner()
print(some_num)
def type_annotation_never_gets_value_despite_nonlocal():
"""Type annotation lacks a value despite nonlocal declaration"""
some_num: int
def inner():
nonlocal some_num
inner()
print(some_num) # [used-before-assignment]
|