Ruff Python Linter and Formatter

repository·main·Indexed 12 days ago

https://github.com/astral-sh/ruff

An extremely fast Python linter and code formatter written in Rust, designed to replace tools like Flake8, Black, and isort. It includes a high-performance engine, a Language Server Protocol (LSP) implementation via `ruff server`, and WASM packages for Web and Node.js environments. The project also provides a hand-written recursive descent parser for transforming Python source code into an Abstract Syntax Tree (AST).

Tokens
896.5K
Snippets
3K
Records
3.5K
Agent score
99%

What's inside Ruff

  1. Overview of Ruff

    main

    Ruff is an extremely fast Python linter and code formatter written in Rust. It is designed to replace multiple existing tools by providing a single, high-performance interface.

    Key features include:

    • Performance: 10-100x faster than existing linters (like Flake8) and formatters (like Black).
    • Tool Consolidation: Replaces Flake8, Black, isort, pydocstyle, pyupgrade, and autoflake.
    • Rule Support: Over 900 built-in rules, including native re-implementations of popular plugins like flake8-bugbear.
    • Automation: Supports automatic error correction (fix support) and built-in caching to avoid re-analyzing unchanged files.
    • Configuration: Supports pyproject.toml and hierarchical/cascading configuration for monorepos.
    • Compatibility: Python 3.14 compatible and provides first-party editor integrations for VS Code and others.
  2. What is the Type Compendium in ty

    main
    The Type Compendium is an educational reference and specification guide for the ty tool's interpretation of the Python type system. It consists of 'fact sheets' that document the behavior, properties, and implementation details of important, interesting, and peculiar types. Developers and users can use this compendium to understand how specific types (such as Never, Object, None, or Any) are handled within the ty ecosystem to ensure consistency with Python specifications.
  3. Overview of the ty CLI

    main

    The ty command is the entrypoint for an extremely fast Python type checker. It provides several subcommands to manage type checking, run a language server, and inspect rules.

    Available Subcommands:

    • ty check: Check a project for type errors.
    • ty server: Start the language server.
    • ty version: Display the current version of ty.
    • ty explain: Explain rules and other parts of ty.
    • ty help: Print help information for ty or a specific subcommand.
    ty <COMMAND>
  4. Understand the Ruff benchmark resources

    main

    The resources directory in the ruff_benchmark crate contains vendored files from real-world Python projects. These files are used to benchmark Ruff's performance against actual production code rather than synthetic benchmarks. This allows for offline benchmark execution and ensures the benchmarks reflect real-world complexity.

    Included resource files include:

    • numpy/globals
    • numpy/ctypeslib.py
    • pypinyin.py
    • pydantic/types.py
    • large/dataset.py
    • tomllib (from CPython 3.12)

    Note that these files are subject to their original licenses.

  5. Ruff Language Server features overview

    main

    The Ruff Language Server provides several key features for Python development:

    • Diagnostic Highlighting: Real-time linting and error highlighting as you edit code.
    • Dynamic Configuration: Automatically refreshes diagnostics when configuration files (pyproject.toml, ruff.toml, or .ruff.toml) are changed, provided the editor supports file watching.
    • Formatting: Formats entire documents or specific ranges of lines. In VS Code, this is available via the Ruff: Format Document command.
    • Hover: Provides rule documentation when hovering over # noqa comments.
    • Jupyter Notebook Support: Full linting and formatting for .ipynb files.
  6. Which tools does Ruff replace?

    main

    Ruff is designed to replace several popular Python linting and formatting tools.

    Flake8 Replacement: Ruff can replace Flake8 when used with many common plugins, including:

    • flake8-bugbear, flake8-bandit, flake8-comprehensions, flake8-datetimez, flake8-docstrings, flake8-simplify, flake8-type-checking, and many others (see full list in documentation).

    Other Replacements:

    If you rely on an unsupported Flake8 plugin, you can file an issue on GitHub.

  7. Use the ty Language Server for editor integrations

    main

    ty server implements the Language Server Protocol (LSP) to provide editor integrations for the ty language. This allows developers to use ty-specific features (like autocompletion, diagnostics, and navigation) within any editor that supports LSP.

    For detailed information on specific protocol extensions and features supported by the server, refer to the official ty language server documentation.

  8. Understand the changes in Ruff 0.12.x

    main

    The 0.12.x release series introduced several preview features, bug fixes, and rule changes. Key highlights include:

    • New Preview Rules: Expanded support for flake8-use-pathlib (e.g., PTH201, PTH102, PTH103, PTH211), flake8-async (ASYNC212), and airflow specific checks (AIR301, AIR311, AIR312).
    • Rule Behavior Changes: Some rules were updated to be more permissive (e.g., BLE001 permitting logging.critical with exc_info=True) or to mark fixes as unsafe (e.g., E731 for class assignments and PTH100).
    • Output Format Updates: The full output format was improved. Diagnostics now move the filename, line number, and column information to a second line in the header to support future features like sub-diagnostics and multiple annotations.
  9. Use the Ruff Formatter

    main

    The Ruff formatter is an extremely fast Python code formatter that is included as part of the ruff CLI. It is designed as a drop-in replacement for Black, aiming for near-identical output. When run over existing Black-formatted projects, it typically produces > 99.9% identical results.

    Key considerations when migrating from Black:

    • Most code will remain unchanged.
    • Minor differences may occur on the margins; check for intentional deviations if you notice changes.
    • When running on non-Black-formatted code, the formatter may make different decisions, particularly regarding end-of-line comments.
    • For detailed formatting rules, refer to the Ruff Style Guide.