Skip to content

openrewrite/rewrite-python

Repository files navigation

OpenRewrite

rewrite-python

ci Maven Central Revved up by Gradle Enterprise Contributing Guide

Introduction

This repository is a work-in-progress implementation of OpenRewrite Python language support.

Note: For now, this language and the associated recipes are only supported via the Moderne CLI or the Moderne Platform (at least until native build tool support catches up). That being said, the Moderne CLI is free to use for open-source repositories. If your repository is closed-source, though, you will need to obtain a license to use the CLI or the Moderne Platform. Please contact Moderne to learn more.

Getting started

For help getting started with the Moderne CLI, check out our getting started guide. Or, if you'd like to try running these recipes in the Moderne Platform, check out the Moderne Platform quickstart guide.

Contributing

We appreciate all types of contributions. See the contributing guide for detailed instructions on how to get started.

Development information

Implementation progress

This is largely based on the Python grammar specification.

Parser development and testing

The Python parser is being implemented in Python, but its printer is implemented in Java. So regardless of whether the tests are written in Java or in Python, both a Python and a Java process are required to validate the parser's results. As we already have plenty of tests written in Java, this section describes how to run these and also debug the parser. Thus, the idea is to run OpenRewrite JUnit tests, which behind the scenes parse the Python sources using a Python application, where the two applications communicate via TCP sockets (using the LST remoting protocol). In the setup described here it is at the time of writing important that the GitHub repos are cloned in a organization/repo structure.

  1. Clone both moderneinc/rewrite-remote and openrewrite/rewrite-python (here we are assuming ~/git to be the parent directory)
    mkdir -p ~/git/moderneinc && gh repo clone moderneinc/rewrite-remote ~/git/moderneinc/rewrite-remote
    mkdir -p ~/git/openrewrite && gh repo clone openrewrite/rewrite-python ~/git/openrewrite/rewrite-python
  2. Make sure Python 3.12 and Uv are installed
    brew install uv
    uv install python
  3. Create a Python virtual environment (here we are going to use ~/.venv but any directory works) and activate it
    un sync --all-extras
    source ~/.venv/bin/activate
  4. Switch to moderneinc/rewrite-remote/python and install all dependencies using Uv
    cd ~/git/moderneinc/rewrite-remote/python/rewrite-remote-test
    uv build
  5. Open ~/git/openrewrite/rewrite-python/rewrite in IDEA (or PyCharm)
    idea ~/git/openrewrite/rewrite-python/rewrite
  6. Make sure that the Python plugin is installed and also that the interpreter from the created venv is configured and used
  7. Create a new run configuration which runs the Python module rewrite_remote.server (typically in debug mode)
  8. Now open ~/git/openrewrite/rewrite-python in a second IDEA workspace and run the JUnit tests from there

Resources