astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ruff formats Jupyter notebook on save even if Black if default formatter

blazerzar opened this issue · comments

Environment

  • VS Code: 1.85.1
  • OS: macOS Sonoma (same behaviour on EndeavourOS)
  • Ruff extension: v2024.0.0

Settings regarding Python:

{
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter.notebook.ipynb"
    },
    "[python]": {
        "editor.formatOnSave": true,
        "editor.semanticHighlighting.enabled": true,
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        }
    },
    "black-formatter.args": ["--skip-string-normalization"],
    "isort.args":["--profile", "black"],
    "notebook.formatOnSave.enabled": true,
}

Bug
Jupyter notebook is formatted using Ruff (changes single quotes to double quotes) even though it is not the default formatter.

Expected behaviour
Notebook should not be formatted with Ruff.

Currently, I am not using Ruff formatted, because it changes single quotes to double quotes, connected to #6. Because other extensions were giving me some troubles yesterday, I decided to try Ruff again, but made sure that Black is still the default formatter. Everything worked fine in Python scripts, but now I realised that Ruff has been formatting my notebook and changing my single quotes to double.

If I select Format Document from the command palette, Black is shown as default formatter and Ruff is not used. When saving the notebook, Ruff formatting is also visible in the output tab, as well as Black's at the same time.

Hmm... I'm not sure why this is happening, but I'm also not sure that there's much we can do here. Does the Black extension doesn't support formatting notebooks? My guess is that it doesn't, and so Ruff is the only extension reporting as eligible to format them, and so VS Code is using Ruff to format them regardless of what you have set as the default. In other words, this might just be "working as intended" based on VS Code's APIs.

Could I convince you to setup your Ruff configuration to support preserving single quotes? :)

Hmm... I'm not sure why this is happening, but I'm also not sure that there's much we can do here. Does the Black extension doesn't support formatting notebooks? My guess is that it doesn't, and so Ruff is the only extension reporting as eligible to format them, and so VS Code is using Ruff to format them regardless of what you have set as the default. In other words, this might just be "working as intended" based on VS Code's APIs.

Black does support formatting notebooks. I can do it using the command palette or by saving (when Ruff is disabled).

Could I convince you to setup your Ruff configuration to support preserving single quotes? :)

I would love to setup Ruff this way, but am not sure if it is possible currently. I want to have the config inside my VSCode settings, because I use multiple machines and project directories.

Black does support formatting notebooks. I can do it using the command palette or by saving (when Ruff is disabled).

Ah, interesting, I don't know then. Perhaps @karthiknadig has an idea here? It seems like something related to VS Code's APIs or internals, since I don't think we're doing anything special here -- we just format the document when VS Code asks us to format it. But I could be wrong.

I would love to setup Ruff this way, but am not sure if it is possible currently. I want to have the config inside my VSCode settings, because I use multiple machines and project directories.

What I'd suggest here is to create a ruff.toml somewhere on your machine, like ~/ruff.toml or whatever you prefer. Then you can add this to your settings.json:

{
    "ruff.lint.args": ["--config=~/ruff.toml"],
    "ruff.format.args": ["--config=~/ruff.toml"]
}

That way, the settings are used across all your projects and managed in a central place (even though the settings themselves are captured in a TOML file, not in VS Code).

Thank you, perhaps I will go with a ruff.toml file, but I am hoping support for config inside VS Code is added in the future.

At the very least, we're going to support passing arbitrary configuration options via the command line which will let you provide settings like (API TBD, doesn't yet exist):

{
    "ruff.format.args": ["--config quote-style:single"]
}

@charliermarsh VS code treats individual cells like a virtual document. So, in the black extension we format it like a python file. We provide the path to the notebook so black can find and pick config, but provide content for each cell. VS Code parallelly triggers formatting on all cells or the cell currently being worked on depending on the trigger command.

currently there is no way to say format a notebook document. I am looking into that.

@karthiknadig - Just to clarify, I think the user here is asking why VS Code is invoking Ruff when Black is set as the default formatter.

Ah Sorry! Michael (@Yoyokrazy ) might be able to answer that better.

Hi there! Chiming in a bit late to this, but beginning a bit of formatting related work so working through a handful of issues to get an idea of where things are at. I've gone ahead and made an issue in the vscode repo for this, as I think I've pinned down the issue causing default formatters to not be used wrt notebook editors.

In this specific case, Format Notebook is calling against all providers sequentially until a result is returned. Here, Black is returning no edits to the notebook, and then Ruff returns the single edit of changing single to double quotes, and therefore Ruff is used as the provider here. Fix is in progress.

TLDR: Format Cell uses the specified provider, Format Document With... makes you select a provider explicitly, and Format Notebook uses all providers until a result is returned (🐛)

Thanks @Yoyokrazy, much appreciated!

@charliermarsh Today's insiders build should have the latest fix for the above formatting bug, and feel free to ping me directly with anything else notebook/formatting/code action related!