-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add LineSuffix
reserved width
#6830
Conversation
PR Check ResultsBenchmarkLinux
Windows
|
LineSuffix
LineSuffix
LineSuffix
LineSuffix
reserved width
LineSuffix
reserved widthLineSuffix
reserved width
This is exciting. Thanks for taking the time to work on this! |
I started updating the PR description to reflect feedback (see todo list). As discussed on Discord we can also split this PR up if needed in order to unblock dependent work. |
eddc725
to
686f3da
Compare
Was looking at the ecosystem checks. This is unstable atm. Pulled from the transformers repo: class BaseMixedInt8Test(unittest.TestCase):
# ...
EXPECTED_RELATIVE_DIFFERENCE = (
1.540025 # This was obtained on a Quadro RTX 8000 so the number might slightly change
)
From django the same instability occurs with: class Field:
hidden_widget = (
HiddenInput # Default widget to use when rendering this as "hidden".
) Would this imply that FWIW I wanted to just see if this would fix it and it does. diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs
index 52255ec4f..89121e38d 100644
--- a/crates/ruff_python_formatter/src/expression/mod.rs
+++ b/crates/ruff_python_formatter/src/expression/mod.rs
@@ -183,11 +183,12 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
} = self;
let comments = f.context().comments();
- let preserve_parentheses = parenthesize.is_optional()
- && is_expression_parenthesized((*expression).into(), f.context().source());
+ let has_parentheses = is_expression_parenthesized((*expression).into(), f.context().source());
+ let preserve_parentheses = parenthesize.is_optional() && has_parentheses;
- let has_comments =
- comments.has_leading(*expression) || comments.has_trailing_own_line(*expression);
+ let has_comments = comments.has_leading(*expression)
+ || comments.has_trailing_own_line(*expression)
+ || (has_parentheses && comments.has_trailing(*expression));
Maybe that's #6771? |
It seems to me that Black removes the parentheses always. This is a file that I used to play around with the formatting. class BaseMixedInt8Test(unittest.TestCase):
# ...
EXPECTED_RELATIVE_DIFFERENCE = (
1.540025 # This was obtained on a Quadro RTX 8000 so the number might slightly change
)
EXPECTED_RELATIVE_DIFFERENCE = 1.540025 # This was obtained on a Quadro RTX 8000 so the number might slightly change
EXPECTED_RELATIVE_DIFFERENCE = (
1.540025
) # This was obtained on a Quadro RTX 8000 so the number might slightly change
# ...
EXPECTED_RELATIVE_DIFFERENCE = (
1.5 # This was obtained on a Quadro RTX 8000 so the number might slightly change
)
EXPECTED_RELATIVE_DIFFERENCE = 1.5 # This was obtained on a Quadro RTX 8000 so the number might slightly change
EXPECTED_RELATIVE_DIFFERENCE = (
1.5
) # This was obtained on a Quadro RTX 8000 so the number might slightly change
# ...
EXPECTED_RELATIVE_DIFFERENCE = (
1.540025 # This was
)
EXPECTED_RELATIVE_DIFFERENCE = 1.540025 # This was
EXPECTED_RELATIVE_DIFFERENCE = (
1.540025
) # This was
(
EXPECTED_RELATIVE_DIFFERENCE # This was obtained on a Quadro RTX 8000 so the number might slightly change
)= (
1.540025
) # This was I haven't come to a conclusion yet what the best behavior is but we have a few options:
We'll need to play around with this a little more. |
CodSpeed Performance ReportMerging #6830 will not alter performanceComparing Summary
|
Ah I see. I think I was too focused on dealing with the instability. You're right. So is it specific nodes that are given the "break if line suffix exceeds line-lengths" treatment? On first glance it looks like it occurs where some kind of parentheses are present. When I say parentheses I mean Marking as ready to invite some discussion around the solution. I'll continue tinkering. |
I've decided to split this into two PRs. One (this one) will be for adding the new I wanted to do this because the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. My only ask is to use the struct
syntax for StartLineSuffix
and document the field.
Closes #5630
Summary
Currently a trailing, end-of-line comment considered a line suffix element is not measured during formatting. This change introduces a reserved width field to the
ruff_formatter
crate for line suffix elements in order to allow formatter clients to opt-into this behavior.Test Plan
Currently just doctest
Outdated; See (comment)
TODO:
LineSuffix
reserved width (comment)TODO(optional):
TextWidth
instead of internalLineSuffix
(comment)