rewrite-python
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.
- 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
- Make sure Python 3.12 and Uv are installed
brew install uv
cd ~/git/openrewrite/rewrite-python/rewrite
uv python install
- Create a Python virtual environment and activate it
cd ~/git/openrewrite/rewrite-python/rewrite
uv sync --all-extras
source ./.venv/bin/activate
- Switch to
moderneinc/rewrite-remote/python and install all dependencies using Uvcd ~/git/moderneinc/rewrite-remote/python/rewrite-remote
uv build
- Open
~/git/openrewrite/rewrite-python/rewrite in IDEA (or PyCharm)idea ~/git/openrewrite/rewrite-python/rewrite
- Make sure that the Python plugin is installed and also that the interpreter from the created venv is configured and used
- Create a new run configuration which runs the Python module
rewrite_remote.server (typically in debug mode)
- Now open
~/git/openrewrite/rewrite-python in a second IDEA workspace and run the JUnit tests from there
PyCharm setup
(tested using PyCharm 2024.3.1.1 (Community Edition))
In order to conveniently perform development and run tests from within PyCharm one can:
- Open the
~/git/openrewrite/rewrite-python/rewrite as project.
- Configure the virtual environment interpret with:
- PyCharm | Settings | Project: rewrite-python | Python Interpreter | Add Interpreter | Add Local Interpreter
- Select Existing
- Python path - navigate to
~/git/openrewrite/rewrite-python/rewrite/.venv/bin/python
- (Right-click to) mark the top directory as Sources Root (not
rewrite/ subdirectory)
- (Right-click to) mark
rewrite/tests/python/all as Test Sources Root
Resources