Skip to main content

OpenRewrite automated refactoring for Python.

Project description

OpenRewrite Python

OpenRewrite automated refactoring for Python source code.

Installation

pip install openrewrite

Quick Start

from rewrite.python import PythonParser
from rewrite import ExecutionContext

# Parse Python source code
parser = PythonParser()
ctx = ExecutionContext()
source_files = parser.parse(ctx, "example.py")

# Apply recipes to transform code
# ...

Writing Recipes

from dataclasses import dataclass, field
from rewrite import Recipe, option
from rewrite.python import PythonVisitor

@dataclass
class ChangeImport(Recipe):
    """Changes an import from one module to another."""

    old_module: str = field(metadata=option(
        display_name="Old module",
        description="The module to change imports from",
        example="flask"
    ))

    new_module: str = field(metadata=option(
        display_name="New module",
        description="The module to change imports to",
        example="flask_restful"
    ))

    @property
    def name(self) -> str:
        return "org.openrewrite.python.ChangeImport"

    @property
    def display_name(self) -> str:
        return "Change import"

    @property
    def description(self) -> str:
        return "Changes an import from one module to another."

    def editor(self) -> PythonVisitor:
        # Implementation...
        pass

Documentation

See docs.openrewrite.org for full documentation.

License

Moderne Source Available License - see LICENSE.md

Project details


Release history Release notifications | RSS feed

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.84.0.dev20260521112318.tar.gz (303.7 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.84.0.dev20260521112318-py3-none-any.whl (346.3 kB view details)

Uploaded Python 3

File details

Details for the file openrewrite-8.84.0.dev20260521112318.tar.gz.

File metadata

File hashes

Hashes for openrewrite-8.84.0.dev20260521112318.tar.gz
Algorithm Hash digest
SHA256 4acc52450e7ad5be7bd685722140d63e33439585ec232e8351b02d6f306a70cf
MD5 efc1a52c8e3bb9ff991dc12850283335
BLAKE2b-256 c76fc94f904d9de068f618236f983eebdfac5012d9978ec7df649b1d08767235

See more details on using hashes here.

File details

Details for the file openrewrite-8.84.0.dev20260521112318-py3-none-any.whl.

File metadata

File hashes

Hashes for openrewrite-8.84.0.dev20260521112318-py3-none-any.whl
Algorithm Hash digest
SHA256 906627e3193c183c3d253237ac1f6952ef985808b740a63397d716e6ae215554
MD5 e23be4689af88dfca3ae510a8e772c43
BLAKE2b-256 7567b3b57e0529c51abcc77fc0cae575cfe29ce5286e34b77d58b539d82fa427

See more details on using hashes here.

Supported by

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