-
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
Fix zulip unstable formatting with end-of-line comments #6386
Conversation
Current dependencies on/for this PR: This comment was auto-generated by Graphite. |
PR Check ResultsBenchmarkLinux
Windows
|
// Avoid unstable formatting with | ||
// ```python | ||
// x = () - (# | ||
// ) | ||
// ``` | ||
// Without this the comment would go after the empty tuple first, but still expand | ||
// the bin op. In the second formatting pass they are trailing bin op comments | ||
// so the bin op collapse. Suboptimally we keep parentheses around the bin op in | ||
// either case. | ||
(!self.comments[..end_of_line_split].is_empty()).then_some(hard_line_break()), |
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.
What's our motivation to keep the trailing end of line comments on the same line as the opening parentheses?
I prefer
x = () - (
# comment
)
over
x = () - ( # comment
)
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.
Turning an end-of-line comment into an own line comment leads to unstable formatting by the two spaces we insert after the line, so we need turn one comment kind into the other atm.
From a style perspective i prefer black with () # comment
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.
Turning an end-of-line comment into an own line comment leads to unstable formatting by the two spaces we insert after the line,
We shouldn't insert the two empty spaces in that case. We just format them as own line comments.
This isn't possible without moving the comment inside of a = call([ # empty because of x
], b) + [] I prefer: a = call(
[
# empty because of x
],
b
) + [] Over a = call([], b) + [] # empty because of x Because it is now unclear what the comment refers to |
I haven’t reviewed yet but just want to confirm - is this still necessary after my two PRs that get the stability checks passing? |
I think so, this is a slightly different case (empty tuple vs. empty function) that the minimizer stumbled into because you can get to it by removing the function. I'll rebase and check again once the other PRs are merged |
Okay, should be all-clear to rebase. |
crates/ruff_python_formatter/tests/snapshots/format@statement__delete.py.snap
Show resolved
Hide resolved
## Bug Given ```python x = () - (# ) ``` the comment is a dangling comment of the empty tuple. This is an end-of-line comment so it may move after the expression. It still expands the parent, so the operator breaks: ```python x = ( () - () # ) ``` In the next formatting pass, the comment is not a trailing tuple but a trailing bin op comment, so the bin op doesn't break anymore. The comment again expands the parent, so we still add the superfluous parentheses ```python x = ( () - () # ) ``` ## Fix The new formatting is to keep the comment on the empty tuple. This is a log uglier and again has additional outer parentheses, but it's stable: ```python x = ( () - ( # ) ) ``` ## Alternatives Black formats all the examples above as ```python x = () - () # ``` which i find better. I would be happy about any suggestions for better solutions than the current one. I'd mainly need a workaround for expand parent having an effect on the bin op instead of first moving the comment to the end and then applying expand parent to the assign statement.
d5547e9
to
58a33f4
Compare
**Summary** I collected all examples of end-of-line comments after opening parentheses that i could think of so we get a comprehensive view at the state of their formatting (#6390). This PR intentionally only adds tests cases without any changes in formatting. We need to decide which exact formatting we want, ideally in terms of these test files, and implement this in follow-up PRs. ~~One stability check is still deactivated pending #6386
## Bug Given ```python x = () - (# ) ``` the comment is a dangling comment of the empty tuple. This is an end-of-line comment so it may move after the expression. It still expands the parent, so the operator breaks: ```python x = ( () - () # ) ``` In the next formatting pass, the comment is not a trailing tuple but a trailing bin op comment, so the bin op doesn't break anymore. The comment again expands the parent, so we still add the superfluous parentheses ```python x = ( () - () # ) ``` ## Fix The new formatting is to keep the comment on the empty tuple. This is a log uglier and again has additional outer parentheses, but it's stable: ```python x = ( () - ( # ) ) ``` ## Alternatives Black formats all the examples above as ```python x = () - () # ``` which i find better. I would be happy about any suggestions for better solutions than the current one. I'd mainly need a workaround for expand parent having an effect on the bin op instead of first moving the comment to the end and then applying expand parent to the assign statement.
…ral-sh#6420) **Summary** I collected all examples of end-of-line comments after opening parentheses that i could think of so we get a comprehensive view at the state of their formatting (astral-sh#6390). This PR intentionally only adds tests cases without any changes in formatting. We need to decide which exact formatting we want, ideally in terms of these test files, and implement this in follow-up PRs. ~~One stability check is still deactivated pending astral-sh#6386
Bug
Given
the comment is a dangling comment of the empty tuple. This is an end-of-line comment so it may move after the expression. It still expands the parent, so the operator breaks:
In the next formatting pass, the comment is not a trailing tuple but a trailing bin op comment, so the bin op doesn't break anymore. The comment again expands the parent, so we still add the superfluous parentheses
Fix
The new formatting is to keep the comment on the empty tuple. This is a log uglier and again has additional outer parentheses, but it's stable:
Alternatives
Black formats all the examples above as
which i find better.
I would be happy about any suggestions for better solutions than the current one. I'd mainly need a workaround for expand parent having an effect on the bin op instead of first moving the comment to the end and then applying expand parent to the assign statement.