OpenRewrite automated refactoring for Python.
Actively maintained. Last release 2 days ago, commits in 26 of the last 26 weeks. A safe choice.
pip install openrewriteOpenRewrite Python
OpenRewrite automated refactoring for Python source code. This package provides the recipe framework, the Python Lossless Semantic Tree (LST), and the testing helpers you use to author and test Python recipes.
Installation
pip install openrewrite
How it works
OpenRewrite for Python uses a split JVM/Python architecture. You author and unit-test recipes in pure Python, but running a recipe against a real codebase is orchestrated by the JVM runtime via the Moderne CLI (with Python support configured) over an RPC bridge. There is no standalone, in-process Python parser entry point.
Quick start
The fastest way to author and exercise a recipe is the test harness, which parses a before snippet, runs your recipe, and asserts the result matches after:
from rewrite.test import RecipeSpec, python
def test_renames_a_call():
spec = RecipeSpec(recipe=RenameFunctionCall(
old_name="assertEquals",
new_name="assertEqual",
))
spec.rewrite_run(
python("assertEquals(a, b)", "assertEqual(a, b)"),
)
python(before, after) asserts a change; python(before) asserts no change.
data_table asserts on the rows a recipe writes to a data table, given either
the DataTable or its name. The run fails when the recipe writes no rows to it:
def test_reports_deprecated_usages():
def expect_usages(rows):
assert rows == [DeprecatedUsage(source_path="a.py", symbol="foo")]
spec = RecipeSpec(recipe=FindDeprecatedUsages()).data_table(
FindDeprecatedUsages.table, expect_usages)
spec.rewrite_run(python("foo()", "/*~~>*/foo()", path=Path("a.py")))
Writing a recipe
A recipe is a @dataclass subclassing Recipe that returns a visitor from
editor(). Each option must have a default value, or the recipe cannot be
discovered or run.
from dataclasses import dataclass, field
from rewrite import ExecutionContext, Recipe, TreeVisitor, option
from rewrite.java import J
from rewrite.java.tree import MethodInvocation
from rewrite.python.visitor import PythonVisitor
@dataclass
class RenameFunctionCall(Recipe):
"""Rename calls to a function from one name to another."""
old_name: str = field(default="", metadata=option(
display_name="Old function name",
description="The name of the function whose calls should be renamed.",
example="assertEquals",
))
new_name: str = field(default="", metadata=option(
display_name="New function name",
description="The name to rename matching calls to.",
example="assertEqual",
))
@property
def name(self) -> str:
return "com.yourorg.RenameFunctionCall"
@property
def display_name(self) -> str:
return "Rename a function call"
@property
def description(self) -> str:
return "Rename calls to a function from one name to another."
def editor(self) -> TreeVisitor[J, ExecutionContext]:
old_name = self.old_name
new_name = self.new_name
class Visitor(PythonVisitor[ExecutionContext]):
def visit_method_invocation(self, method: MethodInvocation, p: ExecutionContext) -> J:
method = super().visit_method_invocation(method, p)
if method.name.simple_name == old_name:
renamed = method.name.replace(_simple_name=new_name)
return method.replace(_name=renamed)
return method
return Visitor()