Skip to content

Commit

Permalink
utils.rename: Python 3.10+: rename also in match/case captures
Browse files Browse the repository at this point in the history
  • Loading branch information
Technologicat committed Sep 27, 2024
1 parent 6de63b8 commit fbb2bf3
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mcpyrate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit fbb2bf3

Please sign in to comment.