Python bindings for the Ruff code formatter, built with Rust and PyO3.
Maintained. Last release 4 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install ruff-formatruff-format
A fast Python code formatter powered by Ruff's formatting engine.
Overview
ruff-format is a Python package that provides Python bindings to Ruff's Python formatter.
Installation
pip install ruff-format
Usage
from ruff_format import format_string, is_valid_syntax, parse_code
code = """
def hello( x,y, z ):
print( x+y+z )
"""
# Check if code has valid syntax
assert is_valid_syntax(code)
# Parse code (raises RuntimeError on syntax errors)
parse_code(code)
# Format code (default line width: 80)
formatted = format_string(code)
print(formatted)
# Format with a custom line width
formatted = format_string(code, line_width=120)
print(formatted)
Development
Prerequisites
- Rust (latest stable)
- Python 3.8+
- maturin
Building from source
# Install maturin
pip install maturin
# Build the package
maturin develop
# Or build a release version
maturin build --release
License
MIT