Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 24, 2026·3 min de lectura

YAPF — Yet Another Python Formatter by Google

An opinionated Python code formatter that reformats code to the best formatting that conforms to a configured style, even if the original code did not violate the style guide.

Listo para agents

Este activo puede ser leído e instalado directamente por agents

TokRepo expone un comando CLI universal, contrato de instalación, metadata JSON, plan según adaptador y contenido raw para que los agents evalúen compatibilidad, riesgo y próximos pasos.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
YAPF Overview
Comando CLI universal
npx tokrepo install 815b7a4b-578c-11f1-9bc6-00163e2b0d79

Introduction

YAPF takes a different approach from other Python formatters. Rather than only fixing style violations, it reformats the entire file to produce the best formatting conforming to the configured style, similar to how clang-format works for C++. This eliminates debates about formatting during code review.

What YAPF Does

  • Reformats Python code to match a specified style (PEP 8, Google, Chromium, Facebook)
  • Uses a penalty-based algorithm to find optimal line-breaking decisions
  • Handles complex expressions, nested containers, and function call chains
  • Integrates with editors, pre-commit hooks, and CI pipelines
  • Supports partial formatting of selected code regions

Architecture Overview

YAPF parses Python source into a concrete syntax tree, then applies a reformatting algorithm inspired by clang-format. It assigns penalties to line breaks and excess whitespace, then uses dynamic programming to find the lowest-cost layout. The result is deterministic output regardless of the original formatting.

Self-Hosting & Configuration

  • Install from PyPI: pip install yapf
  • Configure via .style.yapf, setup.cfg, or pyproject.toml
  • Set BASED_ON_STYLE to google, pep8, chromium, or facebook as a starting point
  • Override individual knobs like COLUMN_LIMIT, INDENT_WIDTH, SPLIT_BEFORE_LOGICAL_OPERATOR
  • Use # yapf: disable / # yapf: enable comments to skip specific blocks

Key Features

  • Deterministic output: same input always produces same output regardless of original layout
  • Penalty-based algorithm finds globally optimal formatting rather than greedy local fixes
  • Four built-in base styles covering major Python community conventions
  • Supports Python 3.6+ syntax including f-strings and walrus operator
  • Pre-commit hook integration for automatic formatting on git commit

Comparison with Similar Tools

  • Black — zero-configuration philosophy with fewer knobs; YAPF offers more style customization
  • autopep8 — only fixes PEP 8 violations; YAPF reformats entire structure for optimal layout
  • Ruff formatter — extremely fast Rust-based formatter; YAPF is pure Python but more configurable
  • isort — only handles import sorting; YAPF formats all code but can be paired with isort

FAQ

Q: How does YAPF differ from Black? A: Black enforces one style with minimal configuration. YAPF offers many configurable knobs and multiple base styles, letting teams tailor formatting to their preferences.

Q: Can I format only changed lines? A: Yes. Use --lines or --diff-based formatting to only reformat modified regions, useful for large legacy codebases.

Q: Does YAPF support type annotations? A: Yes. YAPF handles PEP 484/526 type annotations, PEP 604 union syntax, and complex generic types.

Q: Is YAPF actively maintained? A: Yes. YAPF is maintained by Google engineers and continues to receive updates for new Python syntax.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados