pyproject-fmt

2.21.1 · active · verified Mon Apr 13

pyproject-fmt is an opinionated formatter for pyproject.toml files, designed to enforce a consistent format similar to how Black formats Python code. It intentionally provides minimal configuration options to establish a single standard format, which helps in reducing configuration time, producing smaller diffs, and simplifying code reviews. The current version is 2.21.1, and it maintains an active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `pyproject-fmt` to format a `pyproject.toml` file. The `run` function processes the file(s) and returns an exit code, where 0 indicates success. It also creates a temporary `pyproject.toml` if one doesn't exist for a runnable example.

import os
from pyproject_fmt import run

# Assuming a pyproject.toml file exists in the current directory for testing
# Create a dummy pyproject.toml for demonstration if it doesn't exist
if not os.path.exists('pyproject.toml'):
    with open('pyproject.toml', 'w') as f:
        f.write('[project]\nname = "my-package"\nversion = "0.1.0"\nrequires-python = ">=3.10"\ndependencies = ["requests"]\n')

# Format a pyproject.toml file and return the exit code
# The 'run' function accepts command-line arguments as a list.
# Pass an empty list or specific file paths.
exit_code = run(["pyproject.toml"])

if exit_code == 0:
    print("pyproject.toml formatted successfully.")
else:
    print(f"Formatting failed with exit code: {exit_code}")

# Clean up the dummy file
if os.path.exists('pyproject.toml'):
    os.remove('pyproject.toml')

view raw JSON →