Skip to content

Commit 113ce84

Browse files
authored
Fix normalize arguments when fstring_formatting is disabled (#13910)
1 parent 7272f83 commit 113ce84

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

crates/ruff_python_formatter/src/other/f_string.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use ruff_python_ast::{AnyStringFlags, FString, StringFlags};
33
use ruff_source_file::Locator;
44

55
use crate::prelude::*;
6-
use crate::preview::{
7-
is_f_string_formatting_enabled, is_f_string_implicit_concatenated_string_literal_quotes_enabled,
8-
};
6+
use crate::preview::is_f_string_formatting_enabled;
97
use crate::string::{Quoting, StringNormalizer, StringQuotes};
108

119
use super::f_string_element::FormatFStringElement;
@@ -33,12 +31,11 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
3331

3432
// If the preview style is enabled, make the decision on what quotes to use locally for each
3533
// f-string instead of globally for the entire f-string expression.
36-
let quoting =
37-
if is_f_string_implicit_concatenated_string_literal_quotes_enabled(f.context()) {
38-
Quoting::CanChange
39-
} else {
40-
self.quoting
41-
};
34+
let quoting = if is_f_string_formatting_enabled(f.context()) {
35+
Quoting::CanChange
36+
} else {
37+
self.quoting
38+
};
4239

4340
let normalizer = StringNormalizer::from_context(f.context()).with_quoting(quoting);
4441

crates/ruff_python_formatter/src/preview.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
1616

1717
/// Returns `true` if the [`f-string formatting`](https://github.com/astral-sh/ruff/issues/7594) preview style is enabled.
1818
/// WARNING: This preview style depends on `is_f_string_implicit_concatenated_string_literal_quotes_enabled`.
19+
/// TODO: Remove `Quoting` when promoting this preview style and convert `FormatStringPart` etc. regular `FormatWithRule` implementations.
20+
/// TODO: Remove `format_f_string` from `normalize_string` when promoting this preview style.
1921
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
2022
context.is_preview()
2123
}

crates/ruff_python_formatter/src/string/implicit.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,9 @@ impl Format<PyFormatContext<'_>> for FormatLiteralContent {
371371
0,
372372
self.flags,
373373
self.flags.is_f_string() && !self.is_fstring,
374-
true,
375-
false,
374+
// TODO: Remove the argument from `normalize_string` when promoting the `is_f_string_formatting_enabled` preview style.
375+
self.flags.is_f_string() && !is_f_string_formatting_enabled(f.context()),
376+
is_f_string_formatting_enabled(f.context()),
376377
);
377378

378379
// Trim the start and end of the string if it's the first or last part of a docstring.

crates/ruff_python_formatter/src/string/normalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ pub(crate) fn normalize_string(
626626
let mut formatted_value_nesting = 0u32;
627627

628628
while let Some((index, c)) = chars.next() {
629-
if matches!(c, '{' | '}') && is_fstring {
629+
if matches!(c, '{' | '}') {
630630
if escape_braces {
631631
// Escape `{` and `}` when converting a regular string literal to an f-string literal.
632632
output.push_str(&input[last_index..=index]);

0 commit comments

Comments
 (0)