-
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 unstable with-items formatting #10274
Conversation
2891e0a
to
6b290c0
Compare
|
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
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with.py
Outdated
Show resolved
Hide resolved
Thanks @aneeshusa 👋 |
25ada0a
to
a069277
Compare
4100b9f
to
a42c365
Compare
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.
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
Just shipped this in v0.3.2 :) |
## 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
Summary
Fixes #10267
The issue with the current formatting is that the formatter flips between the
SingleParenthesizedContextManager
andParenthesizeIfExpands
orSingleWithTarget
because 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
SingleWithoutTarget
layout that uses the same formatting asSingleParenthesizedContextManager
if it has no target and preferSingleWithoutTarget
over usingParenthesizeIfExpands
orSingleWithTarget
.Formatting change
The downside is that we now use
maybe_parenthesize_expression
overparenthesize_if_expands
for expressions wherecan_omit_optional_parentheses
returnsfalse
. 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