summaryrefslogtreecommitdiff
path: root/tests/functional/u/used/used_before_assignment_comprehension_homonyms.py
blob: 2321afed74bb9a96f689aeb58e0595f6527f6233 (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
92
93
94
95
96
"""Homonym between filtered comprehension and assignment in except block."""
# pylint: disable=broad-exception-raised

def func():
    """https://github.com/PyCQA/pylint/issues/5586"""
    try:
        print(value for value in range(1 / 0) if isinstance(value, int))
    except ZeroDivisionError:
        value = 1
        print(value)


def func2():
    """Same, but with attribute access."""
    try:
        print(value for value in range(1 / 0) if isinstance(value.num, int))
    except ZeroDivisionError:
        value = 1
        print(value)


def func3():
    """Same, but with no call."""
    try:
        print(value for value in range(1 / 0) if value)
    except ZeroDivisionError:
        value = 1
        print(value)


def func4():
    """https://github.com/PyCQA/pylint/issues/6035"""
    assets = [asset for asset in range(3) if asset.name == "filename"]

    try:
        raise ValueError
    except ValueError:
        asset = assets[0]
        print(asset)


def func5():
    """Similar, but with subscript notation"""
    results = {}
    # pylint: disable-next=consider-using-dict-items
    filtered =  [k for k in results if isinstance(results[k], dict)]

    try:
        1 / 0
    except ZeroDivisionError:
        k = None
        print(k, filtered)


def func6(data, keys):
    """Similar, but with a subscript in a key-value pair rather than the test
    See https://github.com/PyCQA/pylint/issues/6069"""
    try:
        results = {key: data[key] for key in keys}
    except KeyError as exc:
        key, *_ = exc.args
        raise Exception(f"{key} not found") from exc

    return results


def func7():
    """Similar, but with a comparison"""
    bools = [str(i) == i for i in range(3)]

    try:
        1 / 0
    except ZeroDivisionError:
        i = None
        print(i, bools)


def func8():
    """Similar, but with a container"""
    pairs = [(i, i) for i in range(3)]

    try:
        1 / 0
    except ZeroDivisionError:
        i = None
        print(i, pairs)


# Module level cases

module_ints = [j | j for j in range(3)]
try:
    1 / 0
except ZeroDivisionError:
    j = None
    print(j, module_ints)