This is a quick post on Ruff, a modern, fast linter and formatter for Python. I’ve only recently integrated Ruff into my personal workflow and wanted to collect a few notes about basic usage here.
The official documentation includes a tutorial and a configuration guide that are good starting points. Ruff is both a linter (flagging bugs and bad practices) and a formatter (ensuring stylistic consistency). The boundary between those two functions can be blurred in practice.
Getting started
One can install Ruff via pip install ruff. The configuration is done in the pyproject.toml or a dedicated ruff.toml file, where a set of linting and formatting rules should be defined. The linter is highly customizable this way, whereas the formatter only supports a small set of configurable settings.
In order to list all relevant issues in the current directory, the Ruff linter can be run with the following command:
ruff check
Running ruff check --fix fixes some of the issues automatically. The Ruff formatter immediately starts editing the code when executed:
ruff format
While ruff format --check lists the files that would be reformatted, ruff format --diff displays the modifications that would be applied.
Configuration example
For most of my projects, I start with a minimal Ruff config that complements the implicit default setup. It includes a modern standard for line length and a few other personal style preferences. Additional rules can be gradually enabled as needed during the development process.
Here is an excerpt from the pyproject.toml file:
[tool.ruff]
line-length = 120
indent-width = 4
[tool.ruff.lint]
extend-select = ["E501"] # include line-length violations that would be ignored otherwise (handled by formatter)
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"] # ignore unused imports in __init__.py
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false # do not collapse multi-line collections with a trailing comma into a single line