|
1 | 1 | # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
|
2 | 2 | # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
|
3 | 3 | # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
|
| 4 | +import textwrap |
| 5 | + |
4 | 6 | import pytest
|
5 | 7 |
|
6 |
| -from astroid import ExceptHandler, TryExcept, extract_node |
| 8 | +from astroid import AssignName, ExceptHandler, For, Name, TryExcept, extract_node |
7 | 9 | from astroid.const import PY311_PLUS
|
8 | 10 | from astroid.nodes import TryStar
|
9 | 11 |
|
10 | 12 |
|
11 | 13 | @pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
|
12 | 14 | def test_group_exceptions() -> None:
|
13 | 15 | node = extract_node(
|
14 |
| - """ |
15 |
| -try: |
16 |
| - raise ExceptionGroup("group", [ValueError(654)]) |
17 |
| -except ExceptionGroup as eg: |
18 |
| - for err in eg.exceptions: |
19 |
| - if isinstance(err, ValueError): |
20 |
| - print("Handling ValueError") |
21 |
| - elif isinstance(err, TypeError): |
22 |
| - print("Handling TypeError") |
23 |
| -""" |
| 16 | + textwrap.dedent( |
| 17 | + """ |
| 18 | + try: |
| 19 | + raise ExceptionGroup("group", [ValueError(654)]) |
| 20 | + except ExceptionGroup as eg: |
| 21 | + for err in eg.exceptions: |
| 22 | + if isinstance(err, ValueError): |
| 23 | + print("Handling ValueError") |
| 24 | + elif isinstance(err, TypeError): |
| 25 | + print("Handling TypeError")""" |
| 26 | + ) |
24 | 27 | )
|
25 | 28 | assert isinstance(node, TryExcept)
|
26 | 29 | handler = node.handlers[0]
|
27 | 30 | assert isinstance(handler, ExceptHandler)
|
28 | 31 | assert handler.type.name == "ExceptionGroup"
|
| 32 | + children = list(handler.get_children()) |
| 33 | + assert len(children) == 3 |
| 34 | + exception_group, short_name, for_loop = children |
| 35 | + assert isinstance(exception_group, Name) |
| 36 | + assert isinstance(short_name, AssignName) |
| 37 | + assert isinstance(for_loop, For) |
29 | 38 |
|
30 | 39 |
|
31 | 40 | @pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
|
32 | 41 | def test_star_exceptions() -> None:
|
33 | 42 | node = extract_node(
|
34 |
| - """ |
35 |
| -try: |
36 |
| - raise ExceptionGroup("group", [ValueError(654)]) |
37 |
| -except* ValueError: |
38 |
| - print("Handling ValueError") |
39 |
| -except* TypeError: |
40 |
| - print("Handling TypeError") |
41 |
| -""" |
| 43 | + textwrap.dedent( |
| 44 | + """ |
| 45 | + try: |
| 46 | + raise ExceptionGroup("group", [ValueError(654)]) |
| 47 | + except* ValueError: |
| 48 | + print("Handling ValueError") |
| 49 | + except* TypeError: |
| 50 | + print("Handling TypeError")""" |
| 51 | + ) |
42 | 52 | )
|
43 | 53 | assert isinstance(node, TryStar)
|
44 | 54 | assert node.handlers
|
|
0 commit comments