LinuxCommandLibrary

ruff

Lint and format Python code

TLDR

View documentation for the Ruff linter

$ tldr ruff check
copy

View documentation for the Ruff code formatter
$ tldr ruff format
copy

SYNOPSIS

ruff [COMMAND] [OPTIONS] [PATH...]
ruff [OPTIONS] [PATH...] (defaults to check command)

PARAMETERS

--fix
    Apply all auto-fixable linting violations.

--select <CODES>
    Comma-separated list of rule codes to enable (e.g., `E402,W292`).

--ignore <CODES>
    Comma-separated list of rule codes to ignore (e.g., `E501`).

--exclude <PATH>
    Comma-separated list of file paths to exclude from checking.

--target-version <VERSION>
    Specify the target Python version (e.g., `py38`, `py310`).

--config <FILE>
    Path to a `ruff.toml` or `pyproject.toml` file to use for configuration.

--diff
    Display a diff of changes that would be applied (for `format` command).

--quiet
    Suppress output from linting checks, only showing errors.

--verbose
    Enable verbose output, showing more details during execution.

--force-exclude
    Respect `exclude` patterns even for explicitly provided paths.

--show-fixes
    Show which fixes were applied to the code.

--help
    Display help information for the command or subcommand.

--version
    Print the current ruff version.

DESCRIPTION

ruff is an incredibly fast Python linter and formatter, written in Rust. It aims to replace and consolidate multiple existing tools in the Python ecosystem, including Flake8, isort, and parts of Black and Pylint. By providing a single, performant tool for code quality and formatting, ruff streamlines development workflows. It boasts speeds orders of magnitude faster than its Python-based counterparts, making it ideal for large codebases or continuous integration environments. ruff is highly configurable, allowing users to select or ignore specific linting rules, define formatting styles, and integrate seamlessly into existing `pyproject.toml` configurations. Its primary commands are `check` for linting and `format` for code reformatting, often used together to maintain consistent and high-quality Python code.

CAVEATS

While ruff aims to be a comprehensive replacement, it may not yet support every niche plugin or customization available in older, more established linters. Its rapid development cycle means features and configurations are constantly evolving. It is primarily designed for Python codebases.

COMMON COMMANDS

ruff operates with subcommands:
check: The default command for linting code and identifying issues.
format: Reformat Python code according to a consistent style.
rule: Display information about specific linting rules by their code (e.g., `ruff rule E501`).
config: Interact with ruff's configuration.
linter: List all supported linting rules.
clean: Clear ruff's cache.

CONFIGURATION

ruff is primarily configured via a `pyproject.toml` file under the `[tool.ruff]` section, or alternatively via a `ruff.toml` file. This allows for extensive customization of enabled rules, exclusions, and formatting options.

INTEGRATION

Due to its speed, ruff is highly suitable for integration into Continuous Integration (CI/CD) pipelines, Git hooks, and various Integrated Development Environments (IDEs) like VS Code, PyCharm, and Sublime Text, enhancing code quality checks at various stages of development.

HISTORY

ruff was created by Astral (formerly known as Charlie Marsh) with its first public release in late 2022. It quickly gained significant traction in the Python community due to its unprecedented speed and its ability to consolidate multiple code quality tools into a single, efficient binary. Its development has focused on performance and ease of use, leading to rapid adoption across various projects and organizations.

Copied to clipboard