Coverage for src/renaissance/recipes/python_refactoring.py: 99%
74 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-16 16:04 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-16 16:04 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Base processor for Python-specific source refactoring recipes."""
9import ast
10import importlib
11from collections.abc import Sequence
12from pathlib import Path
13from typing import cast
15from termcolor import colored
17from renaissance.integrations.python.ast.cst_node import PythonCstNode
18from renaissance.integrations.python.ast.factory import PythonFactory, PythonPatternFactory
19from renaissance.integrations.python.ast.rst_node import PythonRstNode
20from renaissance.integrations.python.ast.util import to_str
21from renaissance.syntax_tree import ASTProcessor
22from renaissance.syntax_tree.match_finder import match_pattern
23from renaissance.syntax_tree.semantic_kind import SemanticKind
24from renaissance.utils.text_utils import snake_case
27class PythonRefactoring[NodeT: PythonRstNode | PythonCstNode = PythonRstNode](ASTProcessor):
28 """Base processor for Python-specific source refactoring recipes.
30 NodeT is the node class the file is parsed into: PythonRstNode by default, or PythonCstNode. A subclass picks the
31 CST backend with PythonRefactoring[PythonCstNode] and passes the same class as node_class; NodeT then types body.
32 See PEP 695 (https://peps.python.org/pep-0695/) for the bound and PEP 696 (https://peps.python.org/pep-0696/)
33 for the default.
34 """
36 def __init__(self, file: Path, node_class: type[NodeT] = PythonRstNode) -> None:
37 """Prepare a refactoring processor for file, parsed with node_class (the RST backend by default)."""
38 factory = PythonFactory(node_class)
39 atu = factory.create(file)
40 super().__init__(atu, factory, False)
41 self.pattern_factory = PythonPatternFactory(self.factory)
42 self.black_list_pattern = ".git"
43 self.white_list_pattern = ""
45 def replace_stmt(self, find, repl) -> None:
46 """AI: Replace all statements matching the find pattern with the repl template, expanding captures."""
47 pattern = self.pattern_factory.create_statements(find)
48 for match in match_pattern(self.root.children, pattern):
49 replacement = repl
50 for exp in match.expansions:
51 arg_str = ", ".join([to_str(node) for node in match.expansions[exp]])
52 replacement = replacement.replace(exp, arg_str)
54 replacement = replacement.replace(" ,)", ")").replace(", )", ")")
55 self.replace(replacement, match.nodes, False, False)
57 @staticmethod
58 def process(class_name, file) -> None:
59 """Return a subclass by name using importlib, like Java's Class.forName()."""
60 snake = snake_case(class_name)
61 module = importlib.import_module(f"renaissance.recipes.{snake}")
62 cls = getattr(module, class_name)
63 refactor = cls(file)
64 if refactor.black_list_pattern in refactor.filename or refactor.white_list_pattern not in refactor.filename:
65 print(f"skipping: {Path(refactor.filename).resolve()}")
66 return
68 print(colored(f"refactor {Path(refactor.filename).resolve()}", "green", attrs=["bold"]))
69 refactor.run()
71 def extract_call_arguments(self, node: PythonRstNode) -> tuple[list[str], dict[str, str]]:
72 """Extract positional and keyword arguments from a Call node.
74 The input may be a `Call` node itself or any descendant node.
75 When given a descendant, this method walks up parent links and uses the first
76 ancestor whose semantic kind is `CALL`.
78 Returned keyword arguments preserve Python call semantics where keyword
79 arguments appear after positional arguments.
80 """
81 current = node
82 while current is not None and current.semantic_kind != SemanticKind.CALL:
83 current = current.parent
85 call_node = current
86 if call_node is None:
87 return [], {}
89 args_implicit = next((c for c in call_node.children if c.name == "args"), None)
90 keywords_implicit = next((c for c in call_node.children if c.name == "keywords"), None)
92 positional_args = [arg_node.signature for arg_node in (args_implicit.children if args_implicit else [])]
93 keyword_args: dict[str, str] = {}
94 for kw_node in keywords_implicit.children if keywords_implicit else []:
95 keyword = kw_node.node
96 if isinstance(keyword, ast.keyword) and keyword.arg:
97 value_node = kw_node.children[0] if kw_node.children else kw_node
98 keyword_args[keyword.arg] = value_node.signature
100 return positional_args, keyword_args
102 def class_declares_base(self, class_node: PythonRstNode, base_name: str) -> bool:
103 """Return whether class_node explicitly declares base_name as a base class.
105 This check uses only the names declared in the class header's base list,
106 so implicit Python inheritance from `object` is not treated as a declared base.
107 """
108 return base_name in self.class_base_arguments(class_node)
110 def class_base_arguments(self, class_node: PythonRstNode) -> list[str]:
111 """Return base class signatures explicitly listed in the class declaration.
113 Only names inside the parentheses of ``class Name(...):`` are returned.
114 The implicit default base object is not returned when no bases are declared.
115 """
116 bases_implicit = next((c for c in class_node.children if c.name == "bases"), None)
117 if bases_implicit is None:
118 return []
119 return [child.signature for child in bases_implicit.children]
121 @property
122 def body(self) -> Sequence[NodeT]:
123 """Return the module's top-level statements; with the CST backend, a simple statement comes as its whole line."""
124 root = cast("NodeT", cast("object", self.root))
125 if isinstance(root, PythonCstNode):
126 wrappers = root.translation_unit.wrappers
127 return cast("Sequence[NodeT]", [wrappers[statement] for statement in root.translation_unit.atu.body])
128 return cast("Sequence[NodeT]", root.body)
130 def run(self) -> None:
131 """AI: Run this refactoring recipe. Subclasses override this to perform the refactoring."""