Use Ruff for linting and formatting #99
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Ruff is a Python linter and formatter that has gained popularity due to its high performance and numerous capabilities.
Now that Ruff has released its first minor version series (
0.1
) and has a versioning policy, it's a good time to consider adopting it.This PR will replace Black, Flake8, and isort with Ruff.
Changes
Black
Ruff is a drop-in replacement for Black. No additional configuration is necessary.
Ruff supports opt-in docstring formatting with the
docstring-code-format
setting. The format is compatible with Black string formatting.Flake8
Ruff is a drop-in replacement for Flake8. No additional configuration is necessary.
In addition to replacing Flake8 itself, Ruff can also replace many Flake8 plugins and related projects.
isort
Ruff is not a drop-in replacement for isort at this time. Additional configuration is needed.
extend-select
setting is needed with theI
ruleset.src_paths
setting. Thesrc
setting and theknown-first-party
setting are used instead. The Ruff FAQ currently says, "Ruff accepts asrc
option that in yourpyproject.toml
,ruff.toml
, or.ruff.toml
file, which specifies the directories that Ruff should consider when determining whether an import is first-party," making it sound like onlysrc
would be needed. However, to match isort behavior, bothsrc
andknown-first-party
may need to be set to the same list of directories.ruff check --fix
).Taken together, Ruff configuration settings in
pyproject.toml
related to import sorting look like this:Notes on additional tools
Hatch
Hatch 1.8.0 introduced static analysis with Ruff. Although this is a promising feature of Hatch, it will not be used at this time because of differences between Hatch default settings and Ruff default settings, and because it is not yet possible to fully override the Hatch default settings (though this will likely be improved in the next release, see pypa/hatch#1117 and pypa/hatch#1181).
VSCode
To use Ruff in VSCode, install
charliermarsh.ruff
from the VSCode extension marketplace or Open VSX marketplace (for VSCodium), then updatesettings.json
like this:Related
ruff check
)ruff format
)