Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ Release date: TBA
* Improve ``as_string()`` representation for ``TypeVar``, ``ParamSpec`` and ``TypeVarTuple`` nodes, as well as
type parameter in ``ClassDef``, ``FuncDef`` and ``TypeAlias`` nodes (PEP 695).

* Astroid now correctly supports the ``exceptions`` attribute of ``ExceptionGroup``.

Closes pylint-dev/pylint#8985
Closes pylint-dev/pylint#10558


What's New in astroid 3.3.11?
=============================
Expand Down
7 changes: 7 additions & 0 deletions astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,12 @@ def attr_text(self):
return node_classes.Const("")


class GroupExceptionInstanceModel(ExceptionInstanceModel):
@property
def attr_exceptions(self) -> nodes.Tuple:
return node_classes.Tuple(parent=self._instance)


class OSErrorInstanceModel(ExceptionInstanceModel):
@property
def attr_filename(self):
Expand Down Expand Up @@ -804,6 +810,7 @@ def attr_object(self):

BUILTIN_EXCEPTIONS = {
"builtins.SyntaxError": SyntaxErrorInstanceModel,
"builtins.ExceptionGroup": GroupExceptionInstanceModel,
"builtins.ImportError": ImportErrorInstanceModel,
"builtins.UnicodeDecodeError": UnicodeDecodeErrorInstanceModel,
# These are all similar to OSError in terms of attributes
Expand Down
17 changes: 17 additions & 0 deletions tests/test_group_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
List,
Name,
Try,
Tuple,
Uninferable,
bases,
extract_node,
Expand All @@ -21,6 +22,22 @@
from astroid.nodes import Expr, Raise, TryStar


@pytest.mark.skipif(not PY311_PLUS, reason="Exception group introduced in Python 3.11")
def test_group_exceptions_exceptions() -> None:
node = extract_node(
textwrap.dedent(
"""
try:
raise ExceptionGroup('', [TypeError(), TypeError()])
except ExceptionGroup as eg:
eg.exceptions #@"""
)
)

inferred = node.inferred()[0]
assert isinstance(inferred, Tuple)


@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
def test_group_exceptions() -> None:
node = extract_node(
Expand Down
Loading