mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Fix unstable f-string formatting for expressions containing a trailing comma (#15545)
This commit is contained in:
parent
fdb9f4e404
commit
1ecb7ce645
4 changed files with 45 additions and 0 deletions
|
@ -719,6 +719,7 @@ print(f"{({1, 2, 3}) - ({2})}")
|
||||||
print(f"{1, 2, {3} }")
|
print(f"{1, 2, {3} }")
|
||||||
print(f"{(1, 2, {3})}")
|
print(f"{(1, 2, {3})}")
|
||||||
|
|
||||||
|
|
||||||
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
||||||
print(f"{ {}, }") # A single item tuple gets parenthesized
|
print(f"{ {}, }") # A single item tuple gets parenthesized
|
||||||
print(f"{ {}.values(), }")
|
print(f"{ {}.values(), }")
|
||||||
|
@ -726,3 +727,7 @@ print(f"{ {}, 1 }") # A tuple with multiple elements doesn't get parenthesized
|
||||||
print(f"{ # Tuple with multiple elements that doesn't fit on a single line gets parenthesized
|
print(f"{ # Tuple with multiple elements that doesn't fit on a single line gets parenthesized
|
||||||
{}, 1,
|
{}, 1,
|
||||||
}")
|
}")
|
||||||
|
|
||||||
|
|
||||||
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15536
|
||||||
|
print(f"{ {}, 1, }")
|
||||||
|
|
|
@ -206,6 +206,20 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
||||||
|
|
||||||
pub(crate) fn finish(&mut self) -> FormatResult<()> {
|
pub(crate) fn finish(&mut self) -> FormatResult<()> {
|
||||||
self.result.and_then(|()| {
|
self.result.and_then(|()| {
|
||||||
|
// Don't add a magic trailing comma when formatting an f-string expression
|
||||||
|
// that always must be flat because the `expand_parent` forces enclosing
|
||||||
|
// groups to expand, e.g. `print(f"{(a,)} ")` would format the f-string in
|
||||||
|
// flat mode but the `print` call gets expanded because of the `expand_parent`.
|
||||||
|
if self
|
||||||
|
.fmt
|
||||||
|
.context()
|
||||||
|
.f_string_state()
|
||||||
|
.can_contain_line_breaks()
|
||||||
|
== Some(false)
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(last_end) = self.entries.position() {
|
if let Some(last_end) = self.entries.position() {
|
||||||
let magic_trailing_comma = has_magic_trailing_comma(
|
let magic_trailing_comma = has_magic_trailing_comma(
|
||||||
TextRange::new(last_end, self.sequence_end),
|
TextRange::new(last_end, self.sequence_end),
|
||||||
|
|
|
@ -147,6 +147,17 @@ pub(crate) enum FStringState {
|
||||||
Outside,
|
Outside,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FStringState {
|
||||||
|
pub(crate) fn can_contain_line_breaks(self) -> Option<bool> {
|
||||||
|
match self {
|
||||||
|
FStringState::InsideExpressionElement(context) => {
|
||||||
|
Some(context.can_contain_line_breaks())
|
||||||
|
}
|
||||||
|
FStringState::Outside => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The position of a top-level statement in the module.
|
/// The position of a top-level statement in the module.
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
|
||||||
pub(crate) enum TopLevelStatementPosition {
|
pub(crate) enum TopLevelStatementPosition {
|
||||||
|
|
|
@ -725,6 +725,7 @@ print(f"{({1, 2, 3}) - ({2})}")
|
||||||
print(f"{1, 2, {3} }")
|
print(f"{1, 2, {3} }")
|
||||||
print(f"{(1, 2, {3})}")
|
print(f"{(1, 2, {3})}")
|
||||||
|
|
||||||
|
|
||||||
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
||||||
print(f"{ {}, }") # A single item tuple gets parenthesized
|
print(f"{ {}, }") # A single item tuple gets parenthesized
|
||||||
print(f"{ {}.values(), }")
|
print(f"{ {}.values(), }")
|
||||||
|
@ -732,6 +733,10 @@ print(f"{ {}, 1 }") # A tuple with multiple elements doesn't get parenthesized
|
||||||
print(f"{ # Tuple with multiple elements that doesn't fit on a single line gets parenthesized
|
print(f"{ # Tuple with multiple elements that doesn't fit on a single line gets parenthesized
|
||||||
{}, 1,
|
{}, 1,
|
||||||
}")
|
}")
|
||||||
|
|
||||||
|
|
||||||
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15536
|
||||||
|
print(f"{ {}, 1, }")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Outputs
|
## Outputs
|
||||||
|
@ -1515,6 +1520,7 @@ print(f"{({1, 2, 3}) - ({2})}")
|
||||||
print(f"{1, 2, {3}}")
|
print(f"{1, 2, {3}}")
|
||||||
print(f"{(1, 2, {3})}")
|
print(f"{(1, 2, {3})}")
|
||||||
|
|
||||||
|
|
||||||
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
||||||
print(f"{({},)}") # A single item tuple gets parenthesized
|
print(f"{({},)}") # A single item tuple gets parenthesized
|
||||||
print(f"{({}.values(),)}")
|
print(f"{({}.values(),)}")
|
||||||
|
@ -1527,6 +1533,10 @@ print(
|
||||||
)
|
)
|
||||||
}"
|
}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15536
|
||||||
|
print(f"{ {}, 1 }")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -2310,6 +2320,7 @@ print(f"{({1, 2, 3}) - ({2})}")
|
||||||
print(f"{1, 2, {3}}")
|
print(f"{1, 2, {3}}")
|
||||||
print(f"{(1, 2, {3})}")
|
print(f"{(1, 2, {3})}")
|
||||||
|
|
||||||
|
|
||||||
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15535
|
||||||
print(f"{({},)}") # A single item tuple gets parenthesized
|
print(f"{({},)}") # A single item tuple gets parenthesized
|
||||||
print(f"{({}.values(),)}")
|
print(f"{({}.values(),)}")
|
||||||
|
@ -2322,4 +2333,8 @@ print(
|
||||||
)
|
)
|
||||||
}"
|
}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Regression tests for https://github.com/astral-sh/ruff/issues/15536
|
||||||
|
print(f"{ {}, 1 }")
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue