Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Ruff for linting and formatting #99

Merged
merged 1 commit into from
Jan 14, 2024
Merged

Conversation

br3ndonland
Copy link
Owner

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.

[tool.ruff.format]
docstring-code-format = true

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 setting and the known-first-party setting 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:

[tool.ruff]
extend-select = ["I"]
src = ["package_name", "tests"]

[tool.ruff.lint.isort]
known-first-party = ["package_name", "tests"]

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 update settings.json like this:

{
  "[python]": {
    "editor.codeActionsOnSave": {
      "source.fixAll": "always",
      "source.organizeImports": "always"
    },
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true
  },
  "ruff.lint.args": ["--extend-select=I"]
}

Related

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/
Copy link

vercel bot commented Jan 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
inboard ✅ Ready (Inspect) Visit Preview Jan 14, 2024 1:37am

@br3ndonland br3ndonland merged commit 35e37a7 into develop Jan 14, 2024
38 checks passed
@br3ndonland br3ndonland deleted the br3ndonland/ruff branch January 14, 2024 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant