Skip to content

Commit b51b29b

Browse files
Test children
1 parent b2bc519 commit b51b29b

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

tests/test_group_exceptions.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
22
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
4+
import textwrap
5+
46
import pytest
57

6-
from astroid import ExceptHandler, TryExcept, extract_node
8+
from astroid import AssignName, ExceptHandler, For, Name, TryExcept, extract_node
79
from astroid.const import PY311_PLUS
810
from astroid.nodes import TryStar
911

1012

1113
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
1214
def test_group_exceptions() -> None:
1315
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+
)
2427
)
2528
assert isinstance(node, TryExcept)
2629
handler = node.handlers[0]
2730
assert isinstance(handler, ExceptHandler)
2831
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)
2938

3039

3140
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
3241
def test_star_exceptions() -> None:
3342
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+
)
4252
)
4353
assert isinstance(node, TryStar)
4454
assert node.handlers

0 commit comments

Comments
 (0)