Aller au contenu

Ruff

An extremely fast Python linter and code formatter, written in Rust.

Configuration dans un projet

pyproject.toml
[tool.ruff]
line-length = 88
indent-width = 4
target-version = "py312"

[tool.ruff.lint]
select = ["E", "F"]

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

Configuration globale

~/ruff.toml
line-length = 88
indent-width = 4
target-version = "py311"

[lint]
select = ["E", "F"]

[format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

Utilisation

ruff check           # Run the linter.
ruff format --check  # Run the formatter.

Utilisation avec pre-commit

Configuration

.pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.8.1
    hooks:
      - id: ruff # Run the linter.
      - id: ruff-format # Run the formatter.

Utilisation

pre-commit run --all-files ruff         # Run the linter.
pre-commit run --all-files ruff-format  # Run the formatter.

Intégration à Visual Studio Code

.vscode/extensions.json
{
    "recommendations": [
        "charliermarsh.ruff",
    ],
}
.vscode/settings.json
{
    "[python]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        },
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnSave": true,
        "editor.rulers": [
            88
        ]
    },
    "ruff.path": [
        "${workspaceFolder}/venv/bin/ruff"
    ],
    "ruff.configuration": "${workspaceFolder}/pyproject.toml",
}

Ressources