Skip to main content

A Python annotation formatter for line-wrapping and formatting python comments and docstrings

Project description

pyformatter

CI CI Python 3.11+ License: MIT

pyformatter is a suite of Python formatting tools that automatically formats your docstrings and comments according to configurable style guidelines. It consists of two powerful formatters:

  • pydocfmt: Formats Python docstrings with support for Google-style docstrings
  • pycommentfmt: Formats Python comments to ensure proper line length and readability

Key Features

pydocfmt

  • Google-style docstring formatting: Complete support for Google docstring conventions
  • Multi-line summary handling: Intelligently formats long summaries that span multiple lines
  • Smart section parsing: Properly handles Args, Returns, Raises, Examples, and other sections
  • Code block preservation: Maintains formatting within Examples sections with automatic fencing
  • Type annotation support: Handles parameter type annotations gracefully
  • Blank line management: Ensures proper spacing between summary, description, and sections

pycommentfmt

  • Intelligent comment wrapping: Respects line length while preserving meaning
  • Inline vs block comment handling: Different formatting strategies for different comment types
  • Special comment preservation: Maintains pylint, mypy, and other tool directives
  • Smart spacing: Ensures consistent spacing between code and comments

Table of Contents


Installation

Install pyformatter via pip:

pip install python-doc-formatter

Or install from source:

git clone https://github.com/RikGhosh487/pyformatter.git
cd pyformatter
pip install -e .

Quick Start

Format all Python files in your project:

# Format docstrings
pydocfmt src/

# Format comments  
pycommentfmt src/

# Check formatting without making changes
pydocfmt --check src/
pycommentfmt --check src/

Command Line Usage

pydocfmt

Format Python docstrings with intelligent Google-style docstring support.

pydocfmt [OPTIONS] [FILES/DIRECTORIES]

Options:

  • --line-length INTEGER: Maximum line length for docstrings (default: 88)
  • --check: Check if files are formatted correctly without modifying them
  • --include TEXT: Regex pattern for files to include
  • --exclude TEXT: Regex pattern for files to exclude
  • --help: Show help message and exit

Examples:

# Format specific files
pydocfmt myfile.py another_file.py

# Format entire directory
pydocfmt src/

# Check formatting without changes
pydocfmt --check src/

# Custom line length
pydocfmt --line-length 100 src/

# Include/exclude patterns
pydocfmt --include ".*\.py$" --exclude "test_.*\.py$" src/

pycommentfmt

Format Python comments to ensure proper line length and readability.

pycommentfmt [OPTIONS] [FILES/DIRECTORIES]

Options:

  • --line-length INTEGER: Maximum line length for comments (default: 88)
  • --check: Check if files are formatted correctly without modifying them
  • --include TEXT: Regex pattern for files to include
  • --exclude TEXT: Regex pattern for files to exclude
  • --help: Show help message and exit

Examples:

# Format specific files
pycommentfmt myfile.py

# Format entire directory
pycommentfmt src/

# Check formatting without changes
pycommentfmt --check src/

# Custom line length
pycommentfmt --line-length 79 src/

Configuration

pyformatter can be configured via pyproject.toml:

[tool.pydocfmt]
line_length = 88
exclude = '(^tests/data/|build/)'

[tool.pycommentfmt]
line_length = 88
exclude = '(^tests/data/|build/)'

Configuration Options:

  • line_length: Maximum line length (default: 88)
  • exclude: Regex pattern for files/directories to exclude

Examples

Before and After: pydocfmt

Before:

def calculate_mean(numbers):
    """Calculate the arithmetic mean of a list of numbers.
    
    This function calculates the arithmetic mean of a list of numbers and returns the result as a float value."""
    return sum(numbers) / len(numbers)

After:

def calculate_mean(numbers):
    """Calculate the arithmetic mean of a list of numbers.
    
    This function calculates the arithmetic mean of a list of numbers and returns the
    result as a float value.
    """
    return sum(numbers) / len(numbers)

Before and After: pycommentfmt

Before:

# This is a very long comment that exceeds the line length limit and should be wrapped to multiple lines for better readability
x = 42

After:

# This is a very long comment that exceeds the line length limit and should be
# wrapped to multiple lines for better readability
x = 42

Integration

Pre-commit

Add pyformatter to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/RikGhosh487/pyformatter
    rev: v0.1.0  # Use the ref you want to point at
    hooks:
      - id: pydocfmt
        args: [--line-length=88]
      - id: pycommentfmt
        args: [--line-length=88]

Available hooks:

  • pydocfmt: Format docstrings (modifies files)
  • pydocfmt-check: Check docstring formatting (read-only)
  • pycommentfmt: Format comments (modifies files)
  • pycommentfmt-check: Check comment formatting (read-only)

Common configurations:

# Basic usage
- id: pydocfmt
- id: pycommentfmt

# Custom line length
- id: pydocfmt
  args: [--line-length=100]

# Check only (for CI)
- id: pydocfmt-check
- id: pycommentfmt-check

# With file exclusions
- id: pydocfmt
  args: [--exclude=tests/.*]

Editor Integration

pyformatter works great with:

  • VS Code: Use with the Python extension
  • PyCharm: Configure as an external tool
  • Vim/Neovim: Integrate with formatting plugins

Why pyformatter?

  • Uncompromising: Consistent formatting across your entire codebase
  • Fast: Efficiently processes large codebases
  • Configurable: Adapt to your team's style preferences
  • Reliable: Extensively tested with comprehensive test suite
  • Simple: Easy to integrate into existing workflows

Security

Security is important to us. If you discover a security vulnerability, please report it responsibly by following our Security Policy.

For general security best practices when using pyformatter:

  • Always review changes made by pyformatter before committing
  • Keep pyformatter updated to the latest version
  • When processing untrusted code, consider running pyformatter in an isolated environment

Contributing

Contributions are welcome! We appreciate bug reports, feature requests, documentation improvements, and code contributions.

For detailed information on how to contribute, please see our Contributing Guide.

Quick Start for Contributors:

  1. Fork the repository and clone your fork
  2. Set up the development environment: pip install -e .[dev]
  3. Install pre-commit hooks: pre-commit install
  4. Make your changes and add tests
  5. Run the test suite: python -m unittest discover -s tests -v
  6. Submit a pull request

For bug reports and feature requests, please open an issue.


License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments

Inspired by the excellent work of:

  • Black - The uncompromising Python code formatter
  • isort - A Python utility to sort imports
  • docformatter - Formats docstrings to follow conventions

pyformatter: Because every comment and docstring deserves to be beautiful.

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

python_doc_formatter-0.1.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

python_doc_formatter-0.1.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file python_doc_formatter-0.1.1.tar.gz.

File metadata

  • Download URL: python_doc_formatter-0.1.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for python_doc_formatter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c696feb2bd383f99387fb903bbaa4522e762b28f63f32931c94a84b1ac5e5fdb
MD5 5598ae371cfe045db15425fbeb1ca361
BLAKE2b-256 8785e6e5203038ab8c0421609629be4bf1bc541cb69e8cadf7942ac3d7798546

See more details on using hashes here.

File details

Details for the file python_doc_formatter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_doc_formatter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 05ffea4ca08e0ab1c63a5bf9989555c5a2d07d0195b46bf0e09b2da9fb4b2908
MD5 06a4cafb6c23a53503162f6cbabe4544
BLAKE2b-256 64f5eb85ac244b278290c6fe146f86bf7ecdd03db49738250c1584b48404586e

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