LinuxCommandLibrary

ruff-check

Lint and format Python code

TLDR

Run the linter on the given files or directories

$ ruff check [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Apply the suggested fixes, modifying the files in-place
$ ruff check --fix
copy

Run the linter and re-lint on change
$ ruff check --watch
copy

Only enable the specified rules (or all rules), ignoring the configuration file
$ ruff check --select [ALL|rule_code1,rule_code2,...]
copy

Additionally enable the specified rules
$ ruff check --extend-select [rule_code1,rule_code2,...]
copy

Disable the specified rules
$ ruff check --ignore [rule_code1,rule_code2,...]
copy

Ignore all existing violations of a rule by adding # noqa directives to all lines that violate it
$ ruff check --select [rule_code] --add-noqa
copy

SYNOPSIS

`ruff check [path ...] [options]`

path ...: One or more files or directories to check. If omitted, the current working directory is checked.
options: Optional flags and arguments to control linting behavior.

PARAMETERS

path ...
    Specifies the files or directories to lint. If not provided, Ruff will lint the current working directory recursively.

--fix
    Automatically fix all fixable violations. Not all violations are fixable. Use with caution and review changes.

--select
    Comma-separated list of rule codes to enable. Overrides rules specified in configuration files.

--ignore
    Comma-separated list of rule codes to ignore. Overrides rules specified in configuration files.

--exclude
    Comma-separated list of path patterns to exclude from checking.

--target-version
    The Python version to target, e.g., 'py38', 'py39'. Affects rules that depend on Python syntax or features.

--config
    Path to a pyproject.toml file to use for configuration instead of discovering one.

--format
    The output format for violations (e.g., 'text', 'json', 'github', 'grouped').

--watch
    Watch files for changes and re-run checks automatically.

--verbose
    Enable verbose output, showing more details about the linting process.

--silent
    Suppress all non-error output, only showing violations or errors.

DESCRIPTION

The `ruff check` command is the core linting functionality of Ruff, an extremely fast Python linter and code formatter written in Rust. It rapidly analyzes Python source code to identify and report on various issues, including stylistic inconsistencies, potential bugs, unused imports, and common programming errors. Designed as a high-performance alternative to traditional Python linters like Flake8 and Pylint, `ruff check` aims to drastically reduce linting times, making it ideal for large codebases and continuous integration/continuous deployment (CI/CD) pipelines. It supports a vast array of rules, many of which are compatible with existing tools, and offers extensive configurability via `pyproject.toml` files. By enforcing code standards and catching issues early, `ruff check` helps developers maintain high-quality, consistent, and maintainable Python code.

CAVEATS

While `ruff check` is highly effective, users should be aware of a few points:

Completeness: It doesn't cover every single rule from all possible linters (e.g., Pylint has many highly specific rules Ruff might not replicate directly).
Configuration: Optimal usage often requires a well-defined `pyproject.toml` file, which can take some initial setup effort.
Auto-fixing: The `--fix` option is powerful but should be used carefully, and generated changes should always be reviewed before committing.

CONFIGURATION

The primary way to configure `ruff check` is through a `pyproject.toml` file in your project's root or a parent directory. This file allows you to specify enabled/disabled rules, exclude paths, define per-file settings, and much more.

PERFORMANCE

One of Ruff's most significant advantages is its performance. Written in Rust, it can lint massive codebases in milliseconds, significantly reducing development friction and enabling faster feedback in CI/CD environments compared to Python-native alternatives.

PRE-COMMIT HOOKS

Due to its speed, `ruff check` is commonly integrated into pre-commit hooks (e.g., using the `pre-commit` framework). This ensures that code is linted and, optionally, auto-fixed before commits are made, enforcing code quality standards consistently across a team.

HISTORY

Ruff, including the `check` command, was created by Charlie Marsh and first publicly released in late 2022. It quickly gained significant traction within the Python community due to its unprecedented speed and comprehensive rule set, aiming to consolidate and replace a multitude of existing Python linting and formatting tools. Its development in Rust was a key factor in achieving its high performance, addressing a long-standing need for faster feedback loops in Python development workflows. The command has seen rapid evolution, continually adding new rules and features, solidifying its position as a leading tool for Python code quality.

SEE ALSO

flake8(1), pylint(1), black(1), isort(1), git(1)

Copied to clipboard