Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ruff is a Python linter and formatter that has gained popularity due to its high performance and numerous capabilities. https://docs.astral.sh/ruff/ Now that Ruff has released its first minor version series and has a versioning policy, it's a good time to consider adopting it. https://docs.astral.sh/ruff/versioning/ https://astral.sh/blog/ruff-v0.1.0 This commit will replace Black, Flake8, and isort with Ruff. Black Ruff is a drop-in replacement for Black. No additional configuration is necessary. Ruff supports opt-in docstring formatting with `docstring-code-format`. The format is compatible with Black string formatting. ```toml [tool.ruff.format] docstring-code-format = true ``` https://docs.astral.sh/ruff/faq/ https://docs.astral.sh/ruff/formatter/#docstring-formatting https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#strings 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. 1. Import sorting is opt-in. Ruff will not automatically sort imports. The `extend-select` setting is needed with the `I` ruleset. 2. There is no `src_paths` setting. The `src` and `known-first-party` settings are used instead. The Ruff FAQ currently says, "Ruff accepts a `src` option that in your `pyproject.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 only `src` would be needed. However, to match isort behavior, both `src` and `known-first-party` may need to be set to the same list of directories. 3. Although isort is a formatter, Ruff does not run import sorting with the Ruff formatter. The Ruff linter is used instead (`ruff check --fix`). Taken together, Ruff configuration settings in `pyproject.toml` related to import sorting look like this: ```toml [tool.ruff] extend-select = ["I"] src = ["package_name", "tests"] [tool.ruff.lint.isort] known-first-party = ["package_name", "tests"] ``` https://docs.astral.sh/ruff/faq/ https://docs.astral.sh/ruff/settings/ https://docs.astral.sh/ruff/rules/#isort-i https://pycqa.github.io/isort/docs/configuration/options.html#src-paths 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). https://hatch.pypa.io/1.9/config/static-analysis/#default-settings https://docs.astral.sh/ruff/configuration/ https://hatch.pypa.io/latest/blog/2023/12/11/hatch-v180/#static-analysis VSCode To use Ruff in VSCode, install `charliermarsh.ruff` from the VSCode extension marketplace or Open VSX marketplace (for VSCodium), then update `settings.json` like this: ```json { "[python]": { "editor.codeActionsOnSave": { "source.fixAll": "always", "source.organizeImports": "always" }, "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true }, "ruff.lint.args": ["--extend-select=I"] } ``` https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff https://open-vsx.org/extension/charliermarsh/ruff https://vscodium.com/
- Loading branch information