Skip to content

Commit 7348293

Browse files
authored
Various TryStar fixes (#2142)
1 parent c312218 commit 7348293

File tree

9 files changed

+41
-8
lines changed

9 files changed

+41
-8
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in astroid 2.15.4?
1212
=============================
1313
Release date: TBA
1414

15+
* Add visitor function for ``TryStar`` to ``AsStringVisitor`` and
16+
add ``TryStar`` to ``astroid.nodes.ALL_NODE_CLASSES``.
17+
18+
Refs #2142
1519

1620

1721
What's New in astroid 2.15.3?

astroid/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
Subscript,
166166
TryExcept,
167167
TryFinally,
168+
TryStar,
168169
Tuple,
169170
UnaryOp,
170171
Unknown,

astroid/node_classes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
Subscript,
7777
TryExcept,
7878
TryFinally,
79+
TryStar,
7980
Tuple,
8081
UnaryOp,
8182
Unknown,

astroid/nodes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
Subscript,
198198
TryExcept,
199199
TryFinally,
200+
TryStar,
200201
Tuple,
201202
UnaryOp,
202203
Unknown,
@@ -291,6 +292,7 @@
291292
"Subscript",
292293
"TryExcept",
293294
"TryFinally",
295+
"TryStar",
294296
"Tuple",
295297
"UnaryOp",
296298
"Unknown",

astroid/nodes/as_string.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from collections.abc import Iterator
1010
from typing import TYPE_CHECKING
1111

12+
from astroid import nodes
13+
1214
if TYPE_CHECKING:
1315
from astroid.nodes import Const
1416
from astroid.nodes.node_classes import (
@@ -254,13 +256,16 @@ def visit_emptynode(self, node) -> str:
254256
return ""
255257

256258
def visit_excepthandler(self, node) -> str:
259+
n = "except"
260+
if isinstance(getattr(node, "parent", None), nodes.TryStar):
261+
n = "except*"
257262
if node.type:
258263
if node.name:
259-
excs = f"except {node.type.accept(self)} as {node.name.accept(self)}"
264+
excs = f"{n} {node.type.accept(self)} as {node.name.accept(self)}"
260265
else:
261-
excs = f"except {node.type.accept(self)}"
266+
excs = f"{n} {node.type.accept(self)}"
262267
else:
263-
excs = "except"
268+
excs = f"{n}"
264269
return f"{excs}:\n{self._stmt_list(node.body)}"
265270

266271
def visit_empty(self, node) -> str:
@@ -495,6 +500,17 @@ def visit_tryfinally(self, node) -> str:
495500
self._stmt_list(node.body), self._stmt_list(node.finalbody)
496501
)
497502

503+
def visit_trystar(self, node) -> str:
504+
"""return an astroid.TryStar node as string"""
505+
trys = [f"try:\n{self._stmt_list(node.body)}"]
506+
for handler in node.handlers:
507+
trys.append(handler.accept(self))
508+
if node.orelse:
509+
trys.append(f"else:\n{self._stmt_list(node.orelse)}")
510+
if node.finalbody:
511+
trys.append(f"finally:\n{self._stmt_list(node.finalbody)}")
512+
return "\n".join(trys)
513+
498514
def visit_tuple(self, node) -> str:
499515
"""return an astroid.Tuple node as string"""
500516
if len(node.elts) == 1:

astroid/nodes/node_ng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def _get_yield_nodes_skip_lambdas(self):
590590
yield from ()
591591

592592
def _infer_name(self, frame, name):
593-
# overridden for ImportFrom, Import, Global, TryExcept and Arguments
593+
# overridden for ImportFrom, Import, Global, TryExcept, TryStar and Arguments
594594
pass
595595

596596
def _infer(

astroid/rebuilder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ def visit(
507507
) -> nodes.TryExcept | nodes.TryFinally:
508508
...
509509

510+
if sys.version_info >= (3, 11):
511+
512+
@overload
513+
def visit(self, node: ast.TryStar, parent: NodeNG) -> nodes.TryStar:
514+
...
515+
510516
@overload
511517
def visit(self, node: ast.Tuple, parent: NodeNG) -> nodes.Tuple:
512518
...

doc/api/astroid.nodes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Nodes
8080
astroid.nodes.Subscript
8181
astroid.nodes.TryExcept
8282
astroid.nodes.TryFinally
83+
astroid.nodes.TryStar
8384
astroid.nodes.Tuple
8485
astroid.nodes.UnaryOp
8586
astroid.nodes.Unknown
@@ -230,6 +231,8 @@ Nodes
230231

231232
.. autoclass:: astroid.nodes.TryFinally
232233

234+
.. autoclass:: astroid.nodes.TryStar
235+
233236
.. autoclass:: astroid.nodes.Tuple
234237

235238
.. autoclass:: astroid.nodes.UnaryOp

tests/test_group_exceptions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ def test_group_exceptions() -> None:
5454

5555
@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
5656
def test_star_exceptions() -> None:
57-
node = extract_node(
58-
textwrap.dedent(
59-
"""
57+
code = textwrap.dedent(
58+
"""
6059
try:
6160
raise ExceptionGroup("group", [ValueError(654)])
6261
except* ValueError:
@@ -67,9 +66,10 @@ def test_star_exceptions() -> None:
6766
sys.exit(127)
6867
finally:
6968
sys.exit(0)"""
70-
)
7169
)
70+
node = extract_node(code)
7271
assert isinstance(node, TryStar)
72+
assert node.as_string() == code.replace('"', "'").strip()
7373
assert isinstance(node.body[0], Raise)
7474
assert node.block_range(1) == (1, 11)
7575
assert node.block_range(2) == (2, 2)

0 commit comments

Comments
 (0)