summaryrefslogtreecommitdiff
path: root/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return_py38.py
blob: a43a89aa1014f17b98223adaf426338ccbde079f (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
"""Tests for used-before-assignment with assignments in except handlers after
try blocks with return statements.
See: https://github.com/PyCQA/pylint/issues/5500.
"""
# pylint: disable=inconsistent-return-statements


# Named expressions
def func_ok_namedexpr_1(var):
    """'msg' is defined in one handler with a named expression under an if."""
    try:
        return 1 / var.some_other_func()
    except AttributeError:
        if (msg := var.get_msg()):
            pass
    except ZeroDivisionError:
        msg = "Division by 0"
    print(msg)


def func_ok_namedexpr_2(var):
    """'msg' is defined in one handler with a named expression occurring
    in a call used in an if test.
    """
    try:
        return 1 / var.some_other_func()
    except AttributeError:
        if print(msg := var.get_msg()):
            pass
    except ZeroDivisionError:
        msg = "Division by 0"
    print(msg)


def func_ok_namedexpr_3(var):
    """'msg' is defined in one handler with a named expression occurring
    as a keyword in a call used in an if test.
    """
    try:
        return 1 / var.some_other_func()
    except AttributeError:
        if print("zero!", "here", sep=(msg := var.get_sep())):
            pass
    except ZeroDivisionError:
        msg = "Division by 0"
    print(msg)