Skip to content

Commit

Permalink
Various TryStar fixes (#2142)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Apr 24, 2023
1 parent c312218 commit 7348293
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ What's New in astroid 2.15.4?
=============================
Release date: TBA

* Add visitor function for ``TryStar`` to ``AsStringVisitor`` and
add ``TryStar`` to ``astroid.nodes.ALL_NODE_CLASSES``.

Refs #2142


What's New in astroid 2.15.3?
Expand Down
1 change: 1 addition & 0 deletions astroid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
Subscript,
TryExcept,
TryFinally,
TryStar,
Tuple,
UnaryOp,
Unknown,
Expand Down
1 change: 1 addition & 0 deletions astroid/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
Subscript,
TryExcept,
TryFinally,
TryStar,
Tuple,
UnaryOp,
Unknown,
Expand Down
2 changes: 2 additions & 0 deletions astroid/nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
Subscript,
TryExcept,
TryFinally,
TryStar,
Tuple,
UnaryOp,
Unknown,
Expand Down Expand Up @@ -291,6 +292,7 @@
"Subscript",
"TryExcept",
"TryFinally",
"TryStar",
"Tuple",
"UnaryOp",
"Unknown",
Expand Down
22 changes: 19 additions & 3 deletions astroid/nodes/as_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from collections.abc import Iterator
from typing import TYPE_CHECKING

from astroid import nodes

if TYPE_CHECKING:
from astroid.nodes import Const
from astroid.nodes.node_classes import (
Expand Down Expand Up @@ -254,13 +256,16 @@ def visit_emptynode(self, node) -> str:
return ""

def visit_excepthandler(self, node) -> str:
n = "except"
if isinstance(getattr(node, "parent", None), nodes.TryStar):
n = "except*"
if node.type:
if node.name:
excs = f"except {node.type.accept(self)} as {node.name.accept(self)}"
excs = f"{n} {node.type.accept(self)} as {node.name.accept(self)}"
else:
excs = f"except {node.type.accept(self)}"
excs = f"{n} {node.type.accept(self)}"
else:
excs = "except"
excs = f"{n}"
return f"{excs}:\n{self._stmt_list(node.body)}"

def visit_empty(self, node) -> str:
Expand Down Expand Up @@ -495,6 +500,17 @@ def visit_tryfinally(self, node) -> str:
self._stmt_list(node.body), self._stmt_list(node.finalbody)
)

def visit_trystar(self, node) -> str:
"""return an astroid.TryStar node as string"""
trys = [f"try:\n{self._stmt_list(node.body)}"]
for handler in node.handlers:
trys.append(handler.accept(self))
if node.orelse:
trys.append(f"else:\n{self._stmt_list(node.orelse)}")
if node.finalbody:
trys.append(f"finally:\n{self._stmt_list(node.finalbody)}")
return "\n".join(trys)

def visit_tuple(self, node) -> str:
"""return an astroid.Tuple node as string"""
if len(node.elts) == 1:
Expand Down
2 changes: 1 addition & 1 deletion astroid/nodes/node_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def _get_yield_nodes_skip_lambdas(self):
yield from ()

def _infer_name(self, frame, name):
# overridden for ImportFrom, Import, Global, TryExcept and Arguments
# overridden for ImportFrom, Import, Global, TryExcept, TryStar and Arguments
pass

def _infer(
Expand Down
6 changes: 6 additions & 0 deletions astroid/rebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ def visit(
) -> nodes.TryExcept | nodes.TryFinally:
...

if sys.version_info >= (3, 11):

@overload
def visit(self, node: ast.TryStar, parent: NodeNG) -> nodes.TryStar:
...

@overload
def visit(self, node: ast.Tuple, parent: NodeNG) -> nodes.Tuple:
...
Expand Down
3 changes: 3 additions & 0 deletions doc/api/astroid.nodes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Nodes
astroid.nodes.Subscript
astroid.nodes.TryExcept
astroid.nodes.TryFinally
astroid.nodes.TryStar
astroid.nodes.Tuple
astroid.nodes.UnaryOp
astroid.nodes.Unknown
Expand Down Expand Up @@ -230,6 +231,8 @@ Nodes

.. autoclass:: astroid.nodes.TryFinally

.. autoclass:: astroid.nodes.TryStar

.. autoclass:: astroid.nodes.Tuple

.. autoclass:: astroid.nodes.UnaryOp
Expand Down
8 changes: 4 additions & 4 deletions tests/test_group_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ def test_group_exceptions() -> None:

@pytest.mark.skipif(not PY311_PLUS, reason="Requires Python 3.11 or higher")
def test_star_exceptions() -> None:
node = extract_node(
textwrap.dedent(
"""
code = textwrap.dedent(
"""
try:
raise ExceptionGroup("group", [ValueError(654)])
except* ValueError:
Expand All @@ -67,9 +66,10 @@ def test_star_exceptions() -> None:
sys.exit(127)
finally:
sys.exit(0)"""
)
)
node = extract_node(code)
assert isinstance(node, TryStar)
assert node.as_string() == code.replace('"', "'").strip()
assert isinstance(node.body[0], Raise)
assert node.block_range(1) == (1, 11)
assert node.block_range(2) == (2, 2)
Expand Down

0 comments on commit 7348293

Please sign in to comment.