Skip to content

Commit

Permalink
Test children
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Mar 4, 2023
1 parent b2bc519 commit b51b29b
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions tests/test_group_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
import textwrap

import pytest

from astroid import ExceptHandler, TryExcept, extract_node
from astroid import AssignName, ExceptHandler, For, Name, TryExcept, extract_node
from astroid.const import PY311_PLUS
from astroid.nodes import TryStar


@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
def test_group_exceptions() -> None:
node = extract_node(
"""
try:
raise ExceptionGroup("group", [ValueError(654)])
except ExceptionGroup as eg:
for err in eg.exceptions:
if isinstance(err, ValueError):
print("Handling ValueError")
elif isinstance(err, TypeError):
print("Handling TypeError")
"""
textwrap.dedent(
"""
try:
raise ExceptionGroup("group", [ValueError(654)])
except ExceptionGroup as eg:
for err in eg.exceptions:
if isinstance(err, ValueError):
print("Handling ValueError")
elif isinstance(err, TypeError):
print("Handling TypeError")"""
)
)
assert isinstance(node, TryExcept)
handler = node.handlers[0]
assert isinstance(handler, ExceptHandler)
assert handler.type.name == "ExceptionGroup"
children = list(handler.get_children())
assert len(children) == 3
exception_group, short_name, for_loop = children
assert isinstance(exception_group, Name)
assert isinstance(short_name, AssignName)
assert isinstance(for_loop, For)


@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
def test_star_exceptions() -> None:
node = extract_node(
"""
try:
raise ExceptionGroup("group", [ValueError(654)])
except* ValueError:
print("Handling ValueError")
except* TypeError:
print("Handling TypeError")
"""
textwrap.dedent(
"""
try:
raise ExceptionGroup("group", [ValueError(654)])
except* ValueError:
print("Handling ValueError")
except* TypeError:
print("Handling TypeError")"""
)
)
assert isinstance(node, TryStar)
assert node.handlers
Expand Down

0 comments on commit b51b29b

Please sign in to comment.