summaryrefslogtreecommitdiff
path: root/tests/functional/u/used/used_before_assignment_issue2615.py
blob: bce073bf3c114be6ff4c441bc96e6a51f029e493 (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
"""https://github.com/PyCQA/pylint/issues/2615"""
def main():
    """When evaluating except blocks, assume try statements fail."""
    try:
        res = 1 / 0
        res = 42
        if main():
            res = None
        with open(__file__, encoding="utf-8") as opened_file:
            res = opened_file.readlines()
    except ZeroDivisionError:
        print(res)  # [used-before-assignment]
    print(res)


def nested_except_blocks():
    """Assignments in an except are tested against possibly failing
    assignments in try blocks at two different nesting levels."""
    try:
        res = 1 / 0
        res = 42
        if main():
            res = None
        with open(__file__, encoding="utf-8") as opened_file:
            res = opened_file.readlines()
    except ZeroDivisionError:
        try:
            more_bad_division = 1 / 0
        except ZeroDivisionError:
            print(more_bad_division)  # [used-before-assignment]
            print(res)  # [used-before-assignment]
    print(res)


def consecutive_except_blocks():
    """An assignment assumed to execute in one TryExcept should continue to be
    assumed to execute in a consecutive TryExcept.
    """
    try:
        res = 100
    except ZeroDivisionError:
        pass
    try:
        pass
    except ValueError:
        print(res)


def name_earlier_in_except_block():
    """Permit the name that might not have been assigned during the try block
    to be defined inside a conditional inside the except block.
    """
    try:
        res = 1 / 0
    except ZeroDivisionError:
        if main():
            res = 10
        else:
            res = 11
        print(res)