YAPF reformats Python source to a chosen style, inspired by clang-format, via CLI or library APIs with pep8, Google, Facebook, or custom rules.
Overview
YAPF is a Python formatter that analyzes code and chooses line breaks and spacing to match a configured style guide. The goal is output close to what a human would write when following that guide, reducing manual layout work.
It runs as a command-line tool or as FormatCode and FormatFile APIs for editors and CI. Styles can be pep8, yapf, facebook, or files such as .style.yapf, setup.cfg, pyproject.toml, or a home config, with many tunable knobs for indentation, wrapping, and dictionary layout. Exclude patterns can live in .yapfignore or pyproject.toml.
Installation is typically from PyPI or by running the tool directly from a checkout without installing as a package. Python 3.7+ is required. Return codes support CI checks, including zero only when --diff shows no changes needed.
Community editor plugins integrate YAPF into common IDEs. It is Google-owned code but not an official Google product.
Key features
- clang-format-inspired layout algorithm
- pep8, yapf, and facebook presets
- pyproject.toml and .style.yapf config
- FormatCode and FormatFile APIs
- Partial file line-range formatting
- Diff output instead of rewrite
- In-place file formatting option
- yapfignore exclude patterns
Use cases
- Enforcing team Python style in CI
- Reformatting snippets from an IDE
- Applying Google open-source style
- Batch formatting project trees