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

Formatter: deviation from black: dict key split to multiline #11857

Open
minusf opened this issue Jun 13, 2024 · 4 comments
Open

Formatter: deviation from black: dict key split to multiline #11857

minusf opened this issue Jun 13, 2024 · 4 comments
Labels
breaking Breaking API change bug Something isn't working formatter Related to the formatter

Comments

@minusf
Copy link

minusf commented Jun 13, 2024

ruff 0.4.8 python 3.10.14 macos 14.5

[tool.ruff]
line-length = 100

input:

        self.fields["redirect_link"].help_text = "Please do not use Tiny URLs for links to other www.xxxx.xxx pages."

black==24.4.2:

        self.fields["redirect_link"].help_text = (
            "Please do not use Tiny URLs for links to other www.xxxx.xxx pages."
        )

ruff==0.4.8

        self.fields[
            "redirect_link"
        ].help_text = "Please do not use Tiny URLs for links to other www.xxx.xxx pages."
@minusf
Copy link
Author

minusf commented Jun 13, 2024

I couldn't find this one on the list of known deviations, and this was quite disliked with black IIRC.

@MichaReiser MichaReiser added formatter Related to the formatter labels Jun 13, 2024
@minusf
Copy link
Author

minusf commented Jun 13, 2024

also, ruff converted the black output (as well as the input) to the multiline version

@MichaReiser
Copy link
Member

MichaReiser commented Jun 14, 2024

I can confirm that Ruff formats this differently than Black.

Black's behavior is more consistent here in that it always tries to break the right first, before breaking the left. Ruff tries to do the same but I think it doesn't because of BestFitParanthesize's semantic.

Ruff uses BestFitParenthesize for the right side, and the left uses regular groups. The default behavior of the Printer is to prefer breaking right over left, by only testing if what comes after a group contains a line break before exceeding the line width. The problem is, there is no line break according to our current implementation. There's an inline comment that I wrote... but I'm not sure I understand what I meant at that time.

// Don't use the parenthesized with indent layout even when measuring expanded mode similar to `BestFitting`.
// This is to expand the left and not right after the `(` parentheses (it is okay to expand after the content that it wraps).
self.stack.push(TagKind::BestFitParenthesize, args);

Unfortunately, my hope that this would be fixed by #8940 is unjustified. This only changes another small difference.

I'll need to take a closer look at this. Ultimately, I think we can only release this in a new minor version because it will break existing formatting.

@MichaReiser MichaReiser added the bug Something isn't working label Jun 14, 2024
@minusf
Copy link
Author

minusf commented Jun 19, 2024

thank you for looking into this. maybe it's worth adding it to the list of known deviations? it is a quite common occurance...

@zanieb zanieb added the breaking Breaking API change label Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking API change bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

No branches or pull requests

3 participants