From fbb2bf3831242919b26697d99571cf86f3c7bce8 Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Fri, 27 Sep 2024 16:05:47 +0300 Subject: [PATCH] utils.rename: Python 3.10+: rename also in `match`/`case` captures --- mcpyrate/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mcpyrate/utils.py b/mcpyrate/utils.py index 03458ed..b734f7e 100644 --- a/mcpyrate/utils.py +++ b/mcpyrate/utils.py @@ -9,6 +9,7 @@ from contextlib import contextmanager import uuid +from .astcompat import MatchAs, MatchStar from .colorizer import colorize, ColorScheme from . import markers from . import unparser @@ -131,13 +132,17 @@ def transform(self, tree): tree.name = newname if tree.asname == oldname: tree.asname = newname - elif T is ast.ExceptHandler: + elif T is ast.ExceptHandler: # Python 3.11+: `try`/`except*` uses the same `ExceptHandler` AST node type as classic `try`/`except` if tree.name == oldname: tree.name = newname elif T in (ast.Global, ast.Nonlocal): for j in range(len(tree.names)): if tree.names[j] == oldname: tree.names[j] = newname + elif T in (MatchAs, MatchStar): # Python 3.10+: `match`/`case` (pattern matching) + if tree.name == oldname: + tree.name = newname + # Python 3.12+: `type` statement needs no special-casing here, as it uses a `Name` node for its LHS. return self.generic_visit(tree) return Renamer().visit(tree)