Skip to main content

A Python code formatter with custom formatting rules

Project description

Pyhone

CI

A Python code formatter with custom formatting rules that complement Ruff. Pyhone focuses on stylistic rules that go beyond what typical formatters handle.

Features

  • Multiline Spacing Rule: Automatically ensures multi-line statements (3+ lines by default) have blank lines before and after them for better readability
  • Format Mode: Automatically fix formatting issues
  • Check Mode: Report violations without modifying files (useful for CI/CD)
  • GitHub Actions Integration: Output violations in GitHub Actions annotation format
  • Configurable: Customize rules via pyhone.toml
  • Fast: Built in Rust for maximum performance

Installation

cargo build --release

The binary will be available at target/release/pyhone.

Usage

Format Files

Apply fixes to Python files:

pyhone file.py

Check Mode

Report violations without fixing:

pyhone --check file.py

Multiple Files

pyhone file1.py file2.py file3.py

Output Formats

Human-readable (default):

pyhone --format human --check file.py

GitHub Actions:

pyhone --format github --check file.py

Custom Configuration

Specify a custom config file:

pyhone --config my-config.toml file.py

Configuration

Create a pyhone.toml file in your project root:

[rules.multiline-spacing]
# Enable/disable the multiline-spacing rule
enabled = true

# Minimum number of lines for a statement to be considered "multi-line"
# Default: 2
min_lines = 2

Rules

Multiline Spacing

Ensures multi-line statements have blank lines before and after them for improved readability.

Example:

Before:

x = 1
def foo():
    a = 1
    b = 2
    c = 3
    d = 4
y = 2

After:

x = 1

def foo():
    a = 1
    b = 2
    c = 3
    d = 4

y = 2

GitHub Actions Integration

Use Pyhone in your GitHub Actions workflow:

name: Code Quality

on: [push, pull_request]

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Build Pyhone
        run: cargo build --release

      - name: Check Python formatting
        run: |
          find . -name "*.py" -exec ./target/release/pyhone --check --format github {} +

This will automatically annotate your PRs with formatting issues.

Working with Ruff

Pyhone is designed to complement Ruff, not replace it. Use Ruff for:

  • Import sorting
  • Code linting
  • Standard formatting rules

Use Pyhone for:

  • Custom spacing rules
  • Project-specific style enforcement

Example workflow:

# Run Ruff first
ruff check --fix .
ruff format .

# Then run Pyhone
pyhone **/*.py

Development

Running Tests

cargo test

Project Structure

pyhone/
├── src/
│   ├── main.rs              # CLI with clap
│   ├── parser.rs            # Python AST parsing
│   ├── config.rs            # TOML config loading
│   ├── formatter.rs         # Orchestrate rules on source code
│   ├── output.rs            # Output formatters (human, github)
│   └── rules/
│       ├── mod.rs           # Rule trait + registry
│       └── multiline_spacing.rs
└── pyhone.toml             # Example config file

Adding New Rules

  1. Create a new file in src/rules/ (e.g., my_rule.rs)
  2. Implement the FormattingRule trait:
use crate::rules::{FormattingRule, Violation};
use anyhow::Result;
use rustpython_parser::ast::Mod;

pub struct MyRule;

impl FormattingRule for MyRule {
    fn name(&self) -> &str {
        "my-rule"
    }

    fn description(&self) -> &str {
        "Description of my rule"
    }

    fn apply(&self, source: &str, ast: &Mod) -> Result<Vec<Violation>> {
        // Your rule logic here
        Ok(Vec::new())
    }
}
  1. Register the rule in src/rules/mod.rs
  2. Add configuration in src/config.rs
  3. Update the formatter in src/formatter.rs to use your rule

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

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

Source Distribution

pyhone-0.1.4.tar.gz (28.3 kB view details)

Uploaded Source

Built Distributions

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

pyhone-0.1.4-py3-none-win_amd64.whl (1.7 MB view details)

Uploaded Python 3Windows x86-64

pyhone-0.1.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

pyhone-0.1.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.6 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

pyhone-0.1.4-py3-none-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

pyhone-0.1.4-py3-none-macosx_10_12_x86_64.whl (1.7 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

Details for the file pyhone-0.1.4.tar.gz.

File metadata

  • Download URL: pyhone-0.1.4.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for pyhone-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d6601fc1dae97feb58dd37f86b7830986819ea410eae114591354ec457287df9
MD5 7befcb60e72018ee677f2fc37bbf390f
BLAKE2b-256 0cfd34715c4b21f2a831b39e2281639952c87bb12b14e59c86110371509c4598

See more details on using hashes here.

File details

Details for the file pyhone-0.1.4-py3-none-win_amd64.whl.

File metadata

  • Download URL: pyhone-0.1.4-py3-none-win_amd64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for pyhone-0.1.4-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 df8549df4ec7f527df4ae7d3f3e9d784cddc2d3a9d8b84f69a590b1d2673a6ab
MD5 78c62f35391ea50084e6b0783801ca5f
BLAKE2b-256 b191bb89d02982826aea2ef5372e804a521bf01a83ed1537a3124c7f1861b4c1

See more details on using hashes here.

File details

Details for the file pyhone-0.1.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhone-0.1.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e411455e20fb257e6409182b0b5785b1dbf7ef44e8788cb912bff48837fa86f2
MD5 6961aaba7e50f9c535fd95d04813a853
BLAKE2b-256 62b8d359d2ab1cd8681f1bf9cbe0d76ad0e0e30085feb6b09fc4541341aa390c

See more details on using hashes here.

File details

Details for the file pyhone-0.1.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyhone-0.1.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5387c8854a3007e6f6688473507f9c4a1d085ec47e8d678fefe3d63c5df6e353
MD5 26323f172b51a78dc5409a6b3e14e50e
BLAKE2b-256 fd4aee104017b7430b126745744e40434c4013f52b533affcdd23e46763a09e8

See more details on using hashes here.

File details

Details for the file pyhone-0.1.4-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhone-0.1.4-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 42968b59ec9e036468b0ea686b357c18a5872da77ee97f533455012c6a6897c0
MD5 4246f8801c63d4612f4895d6f4cc98b5
BLAKE2b-256 fa40468e4709904a9b64486e98a92d1c3890b0d482f844094634c9237f3accb3

See more details on using hashes here.

File details

Details for the file pyhone-0.1.4-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyhone-0.1.4-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 648c3cc4084ed15ea69c4ad71849ab97bf9be4899606f599be8e6504c103e8fc
MD5 4e389ed714002fa07fe469220ca6206f
BLAKE2b-256 74fbf4831ff98b84e7602a2e96b2c97df341868bc19788939dc7091ccdc55c79

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