mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
Fix bracket spacing for single-element tuples in f-string expressions (#15537)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
This commit is contained in:
parent
556116ee76
commit
9ed67ba33e
3 changed files with 64 additions and 11 deletions
|
@ -718,3 +718,11 @@ print(f"{ {1: 2}.keys() }")
|
||||||
print(f"{({1, 2, 3}) - ({2})}")
|
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
|
||||||
|
print(f"{ {}, }") # A single item tuple gets parenthesized
|
||||||
|
print(f"{ {}.values(), }")
|
||||||
|
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
|
||||||
|
{}, 1,
|
||||||
|
}")
|
||||||
|
|
|
@ -204,18 +204,13 @@ impl Format<PyFormatContext<'_>> for FormatFStringExpressionElement<'_> {
|
||||||
// before the closing curly brace is not strictly necessary, but it's
|
// before the closing curly brace is not strictly necessary, but it's
|
||||||
// added to maintain consistency.
|
// added to maintain consistency.
|
||||||
let bracket_spacing =
|
let bracket_spacing =
|
||||||
match left_most(expression, comments.ranges(), f.context().source()) {
|
needs_bracket_spacing(expression, f.context()).then_some(format_with(|f| {
|
||||||
Expr::Dict(_) | Expr::DictComp(_) | Expr::Set(_) | Expr::SetComp(_) => {
|
if self.context.can_contain_line_breaks() {
|
||||||
Some(format_with(|f| {
|
soft_line_break_or_space().fmt(f)
|
||||||
if self.context.can_contain_line_breaks() {
|
} else {
|
||||||
soft_line_break_or_space().fmt(f)
|
space().fmt(f)
|
||||||
} else {
|
|
||||||
space().fmt(f)
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
_ => None,
|
}));
|
||||||
};
|
|
||||||
|
|
||||||
let item = format_with(|f: &mut PyFormatter| {
|
let item = format_with(|f: &mut PyFormatter| {
|
||||||
// Update the context to be inside the f-string expression element.
|
// Update the context to be inside the f-string expression element.
|
||||||
|
@ -290,3 +285,19 @@ impl Format<PyFormatContext<'_>> for FormatFStringExpressionElement<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn needs_bracket_spacing(expr: &Expr, context: &PyFormatContext) -> bool {
|
||||||
|
// Ruff parenthesizes single element tuples, that's why we shouldn't insert
|
||||||
|
// a space around the curly braces for those.
|
||||||
|
if expr
|
||||||
|
.as_tuple_expr()
|
||||||
|
.is_some_and(|tuple| !tuple.parenthesized && tuple.elts.len() == 1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
matches!(
|
||||||
|
left_most(expr, context.comments().ranges(), context.source()),
|
||||||
|
Expr::Dict(_) | Expr::DictComp(_) | Expr::Set(_) | Expr::SetComp(_)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -724,6 +724,14 @@ print(f"{ {1: 2}.keys() }")
|
||||||
print(f"{({1, 2, 3}) - ({2})}")
|
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
|
||||||
|
print(f"{ {}, }") # A single item tuple gets parenthesized
|
||||||
|
print(f"{ {}.values(), }")
|
||||||
|
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
|
||||||
|
{}, 1,
|
||||||
|
}")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Outputs
|
## Outputs
|
||||||
|
@ -1506,6 +1514,19 @@ print(f"{ {1: 2}.keys() }")
|
||||||
print(f"{({1, 2, 3}) - ({2})}")
|
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
|
||||||
|
print(f"{({},)}") # A single item tuple gets parenthesized
|
||||||
|
print(f"{({}.values(),)}")
|
||||||
|
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
|
||||||
|
(
|
||||||
|
{},
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -2288,4 +2309,17 @@ print(f"{ {1: 2}.keys() }")
|
||||||
print(f"{({1, 2, 3}) - ({2})}")
|
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
|
||||||
|
print(f"{({},)}") # A single item tuple gets parenthesized
|
||||||
|
print(f"{({}.values(),)}")
|
||||||
|
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
|
||||||
|
(
|
||||||
|
{},
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue