Scripts2026年5月24日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
YAPF Overview
直接安装命令
npx -y tokrepo@latest install 815b7a4b-578c-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产