Summary

BetterStack’s hands-on guide to setting up and configuring Ruff, the fast Python linter and formatter written in Rust. Covers installation, pyproject.toml configuration, rule selection (pycodestyle E, pyflakes F, isort I), auto-fixing, and pre-commit hook integration.

BetterStack 關於設置和配置 Ruff 的實踐指南,Ruff 是用 Rust 編寫的快速 Python 代碼檢查器和格式化工具。涵蓋安裝、pyproject.toml 配置、規則選擇(pycodestyle E、pyflakes F、isort I)、自動修復和 pre-commit 鉤子集成。

Key Points

  • Ruff replaces flake8, black, isort, pylint, pyupgrade, and more in a single tool — 10-100x faster than individual tools
  • Written in Rust; speed allows running on every save without noticeable delay
  • Rule categories: E (pycodestyle style errors), F (pyflakes logic errors), I (isort import sorting), and many more
  • Auto-fix: ruff check --fix resolves most issues automatically; ruff format handles formatting
  • Configuration via pyproject.toml [tool.ruff] section: select, ignore, line-length, per-file ignores
  • Import sorting: enabled with I rule group; replaces isort entirely
  • Pre-commit integration: add as a hook to block commits with linting violations

Insights

Ruff’s adoption curve is steep because it eliminates the “which linter to use” decision: it consolidates flake8 + isort + black + many plugins into one tool with consistent configuration. The speed difference (written in Rust vs. Python tooling) matters in practice — linters slow enough to feel laggy get disabled or run only in CI, where they catch things too late. The ruff format subcommand is now recommended as a black replacement in many projects. For new Python projects in 2025, starting with Ruff + pyproject.toml is the obvious choice over assembling a multi-tool setup.

Connections

Raw Excerpt

Ruff is a fast Python linter and code formatter written in Rust that has rapidly gained popularity in the Python ecosystem. It includes all the standard features expected in any linting framework, such as style checking, error detection, and automatic code fixing capabilities.