This release is a pre-release and may not be stable for production use.
OpenRewrite 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.
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()
Returning None from a visit method removes the node entirely — which is how
recipes delete code.
Running recipes with the Moderne CLI
Expose an activate() function so the CLI can discover your recipe:
from rewrite.marketplace import RecipeMarketplace, Python
def activate(marketplace: RecipeMarketplace) -> None:
marketplace.install(RenameFunctionCall, Python)
Then install and run it against a repository whose Python LSTs you've built,
passing each option as a -P parameter:
# From your recipe project directory, install it into the CLI's marketplace:
mod config recipes pip install .
# Build the LSTs for the repository you want to refactor, then run the recipe:
mod build /path/to/your/repo
mod run /path/to/your/repo --recipe=com.yourorg.RenameFunctionCall \
-P old_name=assertEquals -P new_name=assertEqual
Learn more
- Python Recipe Starter — a complete project with example recipes, tests, and CI to clone and build on
- Writing a Python refactoring recipe — step-by-step authoring guide
rewrite-pythonmodule — the Python LST, parser, and built-in recipes- docs.openrewrite.org — full OpenRewrite documentation
License
Moderne Source Available License - see LICENSE.md
Metadata
Release files for openrewrite 8.87.0.dev20260709133645
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openrewrite-8.87.0.dev20260709133645.tar.gz | 322.1 kB | Without provenance | Details for openrewrite-8.87.0.dev20260709133645.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openrewrite-8.87.0.dev20260709133645-py3-none-any.whl | Python 3 | none | any | Details for openrewrite-8.87.0.dev20260709133645-py3-none-any.whl |
Total release size: 685.3 kB
Release files / openrewrite-8.87.0.dev20260709133645.tar.gz
| Download URL | openrewrite-8.87.0.dev20260709133645.tar.gz |
|---|---|
| Size | 322.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e1f91751561267f2a3eed0f99dd8b1a9f58d392fe7779560bf372408d19b096
|
|
BLAKE2b-256 checksum How to use checksums |
75b6b5347e27eada1ac415a9008d7c1b7012ecd1580e6f52be43a197570bc103
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / openrewrite-8.87.0.dev20260709133645-py3-none-any.whl
| Download URL | openrewrite-8.87.0.dev20260709133645-py3-none-any.whl |
|---|---|
| Size | 363.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db909511f9fef08e25b694dc572a4a8fe784108a53513c6c08080c48c890e766
|
|
BLAKE2b-256 checksum How to use checksums |
ef51bedd290c2ea0ff94f3ce47ee5042d3f98c60a018fda5e2da7c6b3c2658e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for openrewrite 8.87.0.dev20260709133645
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.