Skip to content

Commit a4b7c4e

Browse files
committed
[formatter] Stabilize fix for single-with-item formatting with trailing comment (#16603)
## Summary This PR stabilizies the fix for #14001 We try to only make breaking formatting changes once a year. However, the plan was to release this fix as part of Ruff 0.9 but I somehow missed it when promoting all other formatter changes. I think it's worth making an exception here considering that this is a bug fix, it improves readability, and it should be rare (very few files in a single project). Our version policy explicitly allows breaking formatter changes in any minor release and the idea of only making breaking formatter changes once a year is mainly to avoid multiple releases throughout the year that introduce large formatter changes Closes #14001 ## Test Plan Updated snapshot
1 parent df6c850 commit a4b7c4e

File tree

3 files changed

+16
-99
lines changed

3 files changed

+16
-99
lines changed

crates/ruff_python_formatter/src/other/with_item.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::expression::parentheses::{
66
is_expression_parenthesized, parenthesized, Parentheses, Parenthesize,
77
};
88
use crate::prelude::*;
9-
use crate::preview::is_with_single_target_parentheses_enabled;
109

1110
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1211
pub enum WithItemLayout {
@@ -118,11 +117,7 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
118117
// ...except if the with item is parenthesized and it's not the only with item or it has a target.
119118
// Then use the context expression as a preferred breaking point.
120119
let prefer_breaking_context_expression =
121-
if is_with_single_target_parentheses_enabled(f.context()) {
122-
(optional_vars.is_some() || !single) && is_parenthesized
123-
} else {
124-
is_parenthesized
125-
};
120+
(optional_vars.is_some() || !single) && is_parenthesized;
126121

127122
if prefer_breaking_context_expression {
128123
maybe_parenthesize_expression(

crates/ruff_python_formatter/src/preview.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,3 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
1313
) -> bool {
1414
context.is_preview()
1515
}
16-
17-
/// Returns `true` if the bugfix for single-with items with a trailing comment targeting Python 3.9 or newer is enabled.
18-
///
19-
/// See [#14001](https://github.com/astral-sh/ruff/issues/14001)
20-
pub(crate) fn is_with_single_target_parentheses_enabled(context: &PyFormatContext) -> bool {
21-
context.is_preview()
22-
}

crates/ruff_python_formatter/tests/snapshots/format@statement__with.py.snap

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -771,113 +771,42 @@ if True:
771771
772772
# Regression test for https://github.com/astral-sh/ruff/issues/14001
773773
with (
774-
(
775-
open(
776-
"some/path.txt",
777-
"rb",
778-
)
779-
if True
780-
else open("other/path.txt")
774+
open(
775+
"some/path.txt",
776+
"rb",
781777
)
778+
if True
779+
else open("other/path.txt")
782780
# Bar
783781
):
784782
pass
785783
786784
787785
with ( # trailing comment
788-
(
789-
open(
790-
"some/path.txt",
791-
"rb",
792-
)
793-
if True
794-
else open("other/path.txt")
786+
open(
787+
"some/path.txt",
788+
"rb",
795789
)
790+
if True
791+
else open("other/path.txt")
796792
# Bar
797793
):
798794
pass
799795
800796
801797
with (
802-
(
803-
open(
804-
"some/path.txt",
805-
"rb",
806-
)
807-
if True
808-
else open("other/path.txt")
798+
open(
799+
"some/path.txt",
800+
"rb",
809801
)
802+
if True
803+
else open("other/path.txt")
810804
# Bar
811805
):
812806
pass
813807
```
814808

815809

816-
#### Preview changes
817-
```diff
818-
--- Stable
819-
+++ Preview
820-
@@ -377,42 +377,36 @@
821-
822-
# Regression test for https://github.com/astral-sh/ruff/issues/14001
823-
with (
824-
- (
825-
- open(
826-
- "some/path.txt",
827-
- "rb",
828-
- )
829-
- if True
830-
- else open("other/path.txt")
831-
+ open(
832-
+ "some/path.txt",
833-
+ "rb",
834-
)
835-
+ if True
836-
+ else open("other/path.txt")
837-
# Bar
838-
):
839-
pass
840-
841-
842-
with ( # trailing comment
843-
- (
844-
- open(
845-
- "some/path.txt",
846-
- "rb",
847-
- )
848-
- if True
849-
- else open("other/path.txt")
850-
+ open(
851-
+ "some/path.txt",
852-
+ "rb",
853-
)
854-
+ if True
855-
+ else open("other/path.txt")
856-
# Bar
857-
):
858-
pass
859-
860-
861-
with (
862-
- (
863-
- open(
864-
- "some/path.txt",
865-
- "rb",
866-
- )
867-
- if True
868-
- else open("other/path.txt")
869-
+ open(
870-
+ "some/path.txt",
871-
+ "rb",
872-
)
873-
+ if True
874-
+ else open("other/path.txt")
875-
# Bar
876-
):
877-
pass
878-
```
879-
880-
881810
### Output 2
882811
```
883812
indent-style = space

0 commit comments

Comments
 (0)