| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
import textwrap |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from astroid import ( |
| 9 |
Uninferable, |
| 10 |
bases, |
| 11 |
extract_node, |
| 12 |
nodes, |
| 13 |
) |
| 14 |
from astroid.const import PY311_PLUS |
| 15 |
from astroid.context import InferenceContext |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.skipif(not PY311_PLUS, reason="Exception group introduced in Python 3.11") |
| 19 |
def test_group_exceptions_exceptions() -> None: |
| 20 |
node = extract_node(textwrap.dedent(""" |
| 21 |
try: |
| 22 |
raise ExceptionGroup('', [TypeError(), TypeError()]) |
| 23 |
except ExceptionGroup as eg: |
| 24 |
eg.exceptions #@""")) |
| 25 |
|
| 26 |
inferred = node.inferred()[0] |
| 27 |
assert isinstance(inferred, nodes.Tuple) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher") |
| 31 |
def test_group_exceptions() -> None: |
| 32 |
node = extract_node(textwrap.dedent(""" |
| 33 |
try: |
| 34 |
raise ExceptionGroup("group", [ValueError(654)]) |
| 35 |
except ExceptionGroup as eg: |
| 36 |
for err in eg.exceptions: |
| 37 |
if isinstance(err, ValueError): |
| 38 |
print("Handling ValueError") |
| 39 |
elif isinstance(err, TypeError): |
| 40 |
print("Handling TypeError")""")) |
| 41 |
assert isinstance(node, nodes.Try) |
| 42 |
handler = node.handlers[0] |
| 43 |
assert node.block_range(lineno=1) == (1, 9) |
| 44 |
assert node.block_range(lineno=2) == (2, 3) |
| 45 |
assert node.block_range(lineno=5) == (5, 9) |
| 46 |
assert isinstance(handler, nodes.ExceptHandler) |
| 47 |
assert handler.type.name == "ExceptionGroup" |
| 48 |
children = list(handler.get_children()) |
| 49 |
assert len(children) == 3 |
| 50 |
exception_group, short_name, for_loop = children |
| 51 |
assert isinstance(exception_group, nodes.Name) |
| 52 |
assert exception_group.block_range(1) == (1, 4) |
| 53 |
assert isinstance(short_name, nodes.AssignName) |
| 54 |
assert isinstance(for_loop, nodes.For) |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher") |
| 58 |
def test_star_exceptions() -> None: |
| 59 |
code = textwrap.dedent(""" |
| 60 |
try: |
| 61 |
raise ExceptionGroup("group", [ValueError(654)]) |
| 62 |
except* ValueError: |
| 63 |
print("Handling ValueError") |
| 64 |
except* TypeError: |
| 65 |
print("Handling TypeError") |
| 66 |
else: |
| 67 |
sys.exit(127) |
| 68 |
finally: |
| 69 |
sys.exit(0)""") |
| 70 |
node = extract_node(code) |
| 71 |
assert isinstance(node, nodes.TryStar) |
| 72 |
assert node.as_string() == code.replace('"', "'").strip() |
| 73 |
assert isinstance(node.body[0], nodes.Raise) |
| 74 |
assert node.block_range(1) == (1, 11) |
| 75 |
assert node.block_range(2) == (2, 3) |
| 76 |
assert node.block_range(3) == (3, 3) |
| 77 |
assert node.block_range(4) == (4, 5) |
| 78 |
assert node.block_range(5) == (5, 5) |
| 79 |
assert node.block_range(6) == (6, 7) |
| 80 |
assert node.block_range(7) == (7, 7) |
| 81 |
assert node.block_range(8) == (8, 9) |
| 82 |
assert node.block_range(9) == (9, 9) |
| 83 |
assert node.block_range(10) == (10, 11) |
| 84 |
assert node.block_range(11) == (11, 11) |
| 85 |
assert node.handlers |
| 86 |
handler = node.handlers[0] |
| 87 |
assert isinstance(handler, nodes.ExceptHandler) |
| 88 |
assert handler.type.name == "ValueError" |
| 89 |
orelse = node.orelse[0] |
| 90 |
assert isinstance(orelse, nodes.Expr) |
| 91 |
assert orelse.value.args[0].value == 127 |
| 92 |
final = node.finalbody[0] |
| 93 |
assert isinstance(final, nodes.Expr) |
| 94 |
assert final.value.args[0].value == 0 |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher") |
| 98 |
def test_star_exceptions_infer_name() -> None: |
| 99 |
trystar = extract_node(""" |
| 100 |
try: |
| 101 |
1/0 |
| 102 |
except* ValueError: |
| 103 |
pass""") |
| 104 |
name = "arbitraryName" |
| 105 |
context = InferenceContext() |
| 106 |
context.lookupname = name |
| 107 |
stmts = bases._infer_stmts([trystar], context) |
| 108 |
assert list(stmts) == [Uninferable] |
| 109 |
assert context.lookupname == name |
| 110 |
|
| 111 |
|
| 112 |
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher") |
| 113 |
def test_star_exceptions_infer_exceptions() -> None: |
| 114 |
code = textwrap.dedent(""" |
| 115 |
try: |
| 116 |
raise ExceptionGroup("group", [ValueError(654), TypeError(10)]) |
| 117 |
except* ValueError as ve: |
| 118 |
print(e.exceptions) |
| 119 |
except* TypeError as te: |
| 120 |
print(e.exceptions) |
| 121 |
else: |
| 122 |
sys.exit(127) |
| 123 |
finally: |
| 124 |
sys.exit(0)""") |
| 125 |
node = extract_node(code) |
| 126 |
assert isinstance(node, nodes.TryStar) |
| 127 |
inferred_ve = next(node.handlers[0].statement().name.infer()) |
| 128 |
assert inferred_ve.name == "ExceptionGroup" |
| 129 |
assert isinstance(inferred_ve.getattr("exceptions")[0], nodes.Tuple) |
| 130 |
assert ( |
| 131 |
inferred_ve.getattr("exceptions")[0].elts[0].pytype() == "builtins.ValueError" |
| 132 |
) |
| 133 |
|
| 134 |
inferred_te = next(node.handlers[1].statement().name.infer()) |
| 135 |
assert inferred_te.name == "ExceptionGroup" |
| 136 |
assert isinstance(inferred_te.getattr("exceptions")[0], nodes.Tuple) |
| 137 |
assert inferred_te.getattr("exceptions")[0].elts[0].pytype() == "builtins.TypeError" |