Fix unstable with-items formatting#10274
Merged
charliermarsh merged 1 commit intomainfrom Mar 8, 2024
Merged
Conversation
2891e0a to
6b290c0
Compare
Contributor
|
|
I'm traveling today but am happy to give this a try tomorrow! Amazed how quick you put this together, thank you! |
6b290c0 to
25ada0a
Compare
MichaReiser
commented
Mar 7, 2024
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py
Show resolved
Hide resolved
MichaReiser
commented
Mar 7, 2024
MichaReiser
commented
Mar 7, 2024
Member
|
Thanks @aneeshusa 👋 |
25ada0a to
a069277
Compare
4100b9f to
a42c365
Compare
aneeshusa
reviewed
Mar 8, 2024
aneeshusa
left a comment
There was a problem hiding this comment.
Can't do a GitHub approval but tested this and this fixes both an isolated repro and the monorepo at $WORK! Would love to see this in a release.
a42c365 to
f9416ea
Compare
charliermarsh
approved these changes
Mar 8, 2024
Member
|
Just shipped this in v0.3.2 :) |
nkxxll
pushed a commit
to nkxxll/ruff
that referenced
this pull request
Mar 10, 2024
## Summary Fixes astral-sh#10267 The issue with the current formatting is that the formatter flips between the `SingleParenthesizedContextManager` and `ParenthesizeIfExpands` or `SingleWithTarget` because the layouts use incompatible formatting ( `SingleParenthesizedContextManager`: `maybe_parenthesize_expression(context)` vs `ParenthesizeIfExpands`: `parenthesize_if_expands(item)`, `SingleWithTarget`: `optional_parentheses(item)`. The fix is to ensure that the layouts between which the formatter flips when adding or removing parentheses are the same. I do this by introducing a new `SingleWithoutTarget` layout that uses the same formatting as `SingleParenthesizedContextManager` if it has no target and prefer `SingleWithoutTarget` over using `ParenthesizeIfExpands` or `SingleWithTarget`. ## Formatting change The downside is that we now use `maybe_parenthesize_expression` over `parenthesize_if_expands` for expressions where `can_omit_optional_parentheses` returns `false`. This can lead to stable formatting changes. I only found one formatting change in our ecosystem check and, unfortunately, this is necessary to fix the instability (and instability fixes are okay to have as part of minor changes according to our versioning policy) The benefit of the change is that `with` items with a single context manager and without a target are now formatted identically to how the same expression would be formatted in other clause headers. ## Test Plan I ran the ecosystem check locally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #10267
The issue with the current formatting is that the formatter flips between the
SingleParenthesizedContextManagerandParenthesizeIfExpandsorSingleWithTargetbecause the layouts use incompatible formatting (SingleParenthesizedContextManager:maybe_parenthesize_expression(context)vsParenthesizeIfExpands:parenthesize_if_expands(item),SingleWithTarget:optional_parentheses(item).The fix is to ensure that the layouts between which the formatter flips when adding or removing parentheses are the same. I do this by introducing a new
SingleWithoutTargetlayout that uses the same formatting asSingleParenthesizedContextManagerif it has no target and preferSingleWithoutTargetover usingParenthesizeIfExpandsorSingleWithTarget.Formatting change
The downside is that we now use
maybe_parenthesize_expressionoverparenthesize_if_expandsfor expressions wherecan_omit_optional_parenthesesreturnsfalse. This can lead to stable formatting changes. I only found one formatting change in our ecosystem check and, unfortunately, this is necessary to fix the instability (and instability fixes are okay to have as part of minor changes according to our versioning policy)The benefit of the change is that
withitems with a single context manager and without a target are now formatted identically to how the same expression would be formatted in other clause headers.Test Plan
I ran the ecosystem check locally