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 when line splitting subscript assignment #9781

Closed
rhynix opened this issue Feb 2, 2024 · 6 comments
Closed
Labels
formatter Related to the formatter style How should formatted code look

Comments

@rhynix
Copy link

rhynix commented Feb 2, 2024

Ruff splits lines differently when the line length exceeds the configured line length, and the line contains assignment using subscript notation. For instance, when assigning to a dict.

I had a look at the known deviations, and at other open issues, but could not find this deviation.

Input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["bbbbbbbbbbbbbbbbbbbb"] = "ccccccccccc"

Black output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["bbbbbbbbbbbbbbbbbbbb"] = (
    "ccccccccccc"
)

Ruff (ruff format --isolated file.py) output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
    "bbbbbbbbbbbbbbbbbbbb"
] = "ccccccccccc"

Ruff version:

% ruff version
ruff 0.2.0
@rhynix rhynix changed the title Formatter: different line splitting on subscript assignment Formatter: deviation from Black when line splitting subscript assignment Feb 2, 2024
@AlexWaygood AlexWaygood added formatter Related to the formatter style How should formatted code look labels Feb 2, 2024
@rhynix
Copy link
Author

rhynix commented Feb 2, 2024

Black used to use the style ruff currently uses, but was changed late 2022.

This was the issue: psf/black#1498
This was the PR: psf/black#3368

@MichaReiser
Copy link
Member

Thanks for reporting this issue.

Is it possible that you use/used black 24? Or what black version are you using?

@rhynix
Copy link
Author

rhynix commented Feb 2, 2024

Is it possible that you use/used black 24? Or what black version are you using?

That is correct.

% black --version
black, 24.1.1 (compiled: no)
Python (CPython) 3.8.18

@rhynix
Copy link
Author

rhynix commented Feb 2, 2024

I can confirm this change was introduced in Black 24.1:

https://github.com/psf/black/blob/main/CHANGES.md#2410

If an assignment statement is too long, we now prefer splitting on the right-hand side (#3368)

Setting the format.preview setting to true changes this behaviour to match Black.

@dhruvmanila
Copy link
Member

So, Black 24.1 moved a lot of preview style rules into it's stable style (psf/black#4042). We've implemented the same in our own preview style formatting which can be enabled through --preview flag. Refer to the preview docs for more details.

If you run the mentioned code snippet under the preview style then you'd get the same output:

ruff format --preview --diff
--- /Users/dhruv/playground/ruff/formatter/isolated.py
+++ /Users/dhruv/playground/ruff/formatter/isolated.py
@@ -1 +1,3 @@
-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["bbbbbbbbbbbbbbbbbbbb"] = "ccccccccccc"
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["bbbbbbbbbbbbbbbbbbbb"] = (
+    "ccccccccccc"
+)

You can track the progress of the pull request which will promote the same preview style rules into stable style for Ruff here: #9639

I hope this helps :)

@rhynix
Copy link
Author

rhynix commented Feb 2, 2024

So, Black 24.1 moved a lot of preview style rules into it's stable style (psf/black#4042). We've implemented the same in our own preview style formatting which can be enabled through --preview flag. Refer to the preview docs for more details.

If you run the mentioned code snippet under the preview style then you'd get the same output:

ruff format --preview --diff
--- /Users/dhruv/playground/ruff/formatter/isolated.py
+++ /Users/dhruv/playground/ruff/formatter/isolated.py
@@ -1 +1,3 @@
-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["bbbbbbbbbbbbbbbbbbbb"] = "ccccccccccc"
+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["bbbbbbbbbbbbbbbbbbbb"] = (
+    "ccccccccccc"
+)

You can track the progress of the pull request which will promote the same preview style rules into stable style for Ruff here: #9639

I hope this helps :)

It sure does. Thank you for your help.

I'll close this issue, since I believe it does not provide any extra value at this point.

@rhynix rhynix closed this as completed Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter style How should formatted code look
Projects
None yet
Development

No branches or pull requests

4 participants