Skip to content

Commit df07b50

Browse files
mbyrnepr2pre-commit-ci[bot]Pierre-SassoulasDanielNoord
authored
Fix a false positive for not-an-iterable in Pylint. (#2830)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Pierre Sassoulas <[email protected]> Co-authored-by: Daniël van Noord <[email protected]>
1 parent 7c3b481 commit df07b50

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ Release date: TBA
8787
* Improve ``as_string()`` representation for ``TypeVar``, ``ParamSpec`` and ``TypeVarTuple`` nodes, as well as
8888
type parameter in ``ClassDef``, ``FuncDef`` and ``TypeAlias`` nodes (PEP 695).
8989

90+
* Astroid now correctly supports the ``exceptions`` attribute of ``ExceptionGroup``.
91+
92+
Closes pylint-dev/pylint#8985
93+
Closes pylint-dev/pylint#10558
94+
9095

9196
What's New in astroid 3.3.11?
9297
=============================

astroid/interpreter/objectmodel.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,12 @@ def attr_text(self):
770770
return node_classes.Const("")
771771

772772

773+
class GroupExceptionInstanceModel(ExceptionInstanceModel):
774+
@property
775+
def attr_exceptions(self) -> nodes.Tuple:
776+
return node_classes.Tuple(parent=self._instance)
777+
778+
773779
class OSErrorInstanceModel(ExceptionInstanceModel):
774780
@property
775781
def attr_filename(self):
@@ -804,6 +810,7 @@ def attr_object(self):
804810

805811
BUILTIN_EXCEPTIONS = {
806812
"builtins.SyntaxError": SyntaxErrorInstanceModel,
813+
"builtins.ExceptionGroup": GroupExceptionInstanceModel,
807814
"builtins.ImportError": ImportErrorInstanceModel,
808815
"builtins.UnicodeDecodeError": UnicodeDecodeErrorInstanceModel,
809816
# These are all similar to OSError in terms of attributes

tests/test_group_exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
List,
1313
Name,
1414
Try,
15+
Tuple,
1516
Uninferable,
1617
bases,
1718
extract_node,
@@ -21,6 +22,22 @@
2122
from astroid.nodes import Expr, Raise, TryStar
2223

2324

25+
@pytest.mark.skipif(not PY311_PLUS, reason="Exception group introduced in Python 3.11")
26+
def test_group_exceptions_exceptions() -> None:
27+
node = extract_node(
28+
textwrap.dedent(
29+
"""
30+
try:
31+
raise ExceptionGroup('', [TypeError(), TypeError()])
32+
except ExceptionGroup as eg:
33+
eg.exceptions #@"""
34+
)
35+
)
36+
37+
inferred = node.inferred()[0]
38+
assert isinstance(inferred, Tuple)
39+
40+
2441
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
2542
def test_group_exceptions() -> None:
2643
node = extract_node(

0 commit comments

Comments
 (0)