Skip to main content

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

License

Moderne Source Available License - see LICENSE.md

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openrewrite-8.88.0.tar.gz (336.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openrewrite-8.88.0-py3-none-any.whl (377.4 kB view details)

Uploaded Python 3

File details

Details for the file openrewrite-8.88.0.tar.gz.

File metadata

  • Download URL: openrewrite-8.88.0.tar.gz
  • Upload date:
  • Size: 336.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for openrewrite-8.88.0.tar.gz
Algorithm Hash digest
SHA256 7dc23818f9b4b6a8c0170f47c45c0c45169bc3aa4ddb53a2820f13ea6cc8861b
MD5 00e4c87cef3809643fe57292cf0688c9
BLAKE2b-256 4b192f6d938390900b78e6f56d374ffa72f1dbd97620d3b5e9bbcdb6e872f634

See more details on using hashes here.

File details

Details for the file openrewrite-8.88.0-py3-none-any.whl.

File metadata

  • Download URL: openrewrite-8.88.0-py3-none-any.whl
  • Upload date:
  • Size: 377.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for openrewrite-8.88.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1a8fe4cebc1836395a3b722f244b4a94455e9f9e031be23dfc66d3fa973e181
MD5 4841cc21e1a244a795b6a2e55d2cba2f
BLAKE2b-256 1dbe947e6b2384e092b04528022ccda23cd7841584701ac4fd22987464583959

See more details on using hashes here.

Release history Release notifications | RSS feed

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page