summaryrefslogtreecommitdiff
path: root/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py
blob: f915a85cc1e53c4092f20fe5a02a484f52a0aabe (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
"""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


def function():
    """Assume except blocks execute if the try block returns."""
    try:
        success_message = "success message"
        return success_message
    except ValueError:
        failure_message = "failure message"
    finally:
        print(failure_message)  # [used-before-assignment]

    return failure_message


def func_ok(var):
    """'msg' is defined in all ExceptHandlers."""
    try:
        return 1 / var.some_other_func()
    except AttributeError:
        msg = "Attribute not defined"
    except ZeroDivisionError:
        msg = "Devision by 0"
    print(msg)


def func_ok2(var):
    """'msg' is defined in all ExceptHandlers that don't raise an Exception."""
    try:
        return 1 / var.some_other_func()
    except AttributeError as ex:
        raise Exception from ex
    except ZeroDivisionError:
        msg = "Devision by 0"
    print(msg)


def func_ok3(var):
    """'msg' is defined in all ExceptHandlers that don't return."""
    try:
        return 1 / var.some_other_func()
    except AttributeError:
        return
    except ZeroDivisionError:
        msg = "Devision by 0"
    print(msg)