Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various TryStar fixes #2142

Merged
merged 1 commit into from
Apr 24, 2023
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
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