summaryrefslogtreecommitdiff
path: root/tests/functional/n/nonlocal_without_binding.py
blob: 277c7f7ebdea74f3c75ee287def939c173f09cd9 (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
""" Checks that reversed() receive proper argument """
# pylint: disable=missing-docstring,invalid-name,unused-variable
# pylint: disable=too-few-public-methods


def test():
    def parent():
        a = 42

        def stuff():
            nonlocal a

    c = 24

    def parent2():
        a = 42

        def stuff():
            def other_stuff():
                nonlocal a
                nonlocal c


b = 42


def func():
    def other_func():
        nonlocal b  # [nonlocal-without-binding]

    # Case where `nonlocal-without-binding` was not emitted when
    # the nonlocal name was assigned later in the same scope.
    # https://github.com/pylint-dev/pylint/issues/6883
    def other_func2():
        nonlocal c  # [nonlocal-without-binding]
        c = 1


class SomeClass:
    nonlocal x  # [nonlocal-without-binding]

    def func(self):
        nonlocal some_attr  # [nonlocal-without-binding]


def func2():
    nonlocal_ = None
    local = None

    class Class:

        nonlocal nonlocal_

        nonlocal_ = 1
        local = 1

    return local + nonlocal_


def function():
    """Test for `unused-variable` when multiple-assignment contains a `nonlocal`"""
    myint, mylist = 0, []

    print(mylist)

    def inner():
        nonlocal myint
        mylist.append(myint)
        myint += 1

    return inner()