-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
formats assert f(a,) == (b)
and assert f(a,) == b
very differently
#15842
Comments
Thanks for the report. Can you say why it doesn't make sense? @MichaReiser will be able to provide more context on why this is the case but he's currently on leave and will be able to reply next week. Regardless, it matches what Black does https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ACpAGFdAD2IimZxl1N_WlPTQ06-Nur1lMjjikbZBUWzXuAQNKhFphrTQvWGzojFvt24KBUXaI3nTaBMACVKkaNvMxbDMiDTeDauQNv4yFlbtT0lJmLmdtWcjJOxsFmoi2ajLXi1CwAAAAAAM4BGDhyTg2EAAX2qAQAAAJAqwvaxxGf7AgAAAAAEWVo%3D |
It doesn’t make sense for two reasons:
|
I think one part of the reason is the trailing comma that's present in the function call arguments. That indicates the formatter to break the expression, even though it might short and under the configured line length, into multiple lines and keep them as multiline expression. You can opt-out of this behavior by setting |
But the magic trailing comma is an immensely useful parameter that can’t be turned off without major impact on the rest of the code. In any case, the magic comma in |
What you see here is also not specific to Specifically, Ruff supports two layouts for clause headers (which are also used for assert conditions):
You can also see this if you flip the operands: assert f(a,) == b
assert b == f(a, ) The code for this can be found here: ruff/crates/ruff_python_formatter/src/expression/mod.rs Lines 527 to 610 in f3b918d
The layout 1 exists to avoid unnecessary parentheses in common cases and it obviously don't pick the ideal solution in this case. However, this is also a very trivial example where the parentheses around assert f(
a,
) == (
aaaaaaaaaaaaaa
and bbbbbbbbbbbbbb
and cccccccccccc
and dddddddddddddddddd
and eeeeeeeeeeee
)
assert f(
a,
) == (aaaaaaaaaaaaaa and bbbbbbbbbbbbbb and cccccccccccc and dddddddddddddddddd) I do agree that there's some room for improvement here so I'll keep the issue open but I don't expect to change this anytime soon because changing this behavior is a fairly significant break with Black's style guide and there are other "bad formattings" that I want to improve first. |
The same issue in Black: psf/black#2581 |
Description
ruff format transforms
into
which doesn’t make too much sense.
The text was updated successfully, but these errors were encountered: