FString formatting: remove fstring handling in normalize_string (#10119)

This commit is contained in:
Micha Reiser 2024-02-25 18:28:46 +01:00 committed by GitHub
parent 51ce88bb23
commit 1711bca4a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View file

@ -62,6 +62,7 @@ impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
self.context.quotes(), self.context.quotes(),
self.context.prefix(), self.context.prefix(),
is_hex_codes_in_unicode_sequences_enabled(f.context()), is_hex_codes_in_unicode_sequences_enabled(f.context()),
true,
); );
match &normalized { match &normalized {
Cow::Borrowed(_) => source_text_slice(self.element.range()).fmt(f), Cow::Borrowed(_) => source_text_slice(self.element.range()).fmt(f),

View file

@ -7,7 +7,7 @@ use ruff_text_size::{Ranged, TextRange};
use crate::context::FStringState; use crate::context::FStringState;
use crate::options::PythonVersion; use crate::options::PythonVersion;
use crate::prelude::*; use crate::prelude::*;
use crate::preview::is_hex_codes_in_unicode_sequences_enabled; use crate::preview::{is_f_string_formatting_enabled, is_hex_codes_in_unicode_sequences_enabled};
use crate::string::{QuoteChar, Quoting, StringPart, StringPrefix, StringQuotes}; use crate::string::{QuoteChar, Quoting, StringPart, StringPrefix, StringQuotes};
use crate::QuoteStyle; use crate::QuoteStyle;
@ -18,6 +18,7 @@ pub(crate) struct StringNormalizer {
f_string_state: FStringState, f_string_state: FStringState,
target_version: PythonVersion, target_version: PythonVersion,
normalize_hex: bool, normalize_hex: bool,
format_fstring: bool,
} }
impl StringNormalizer { impl StringNormalizer {
@ -29,6 +30,7 @@ impl StringNormalizer {
f_string_state: context.f_string_state(), f_string_state: context.f_string_state(),
target_version: context.options().target_version(), target_version: context.options().target_version(),
normalize_hex: is_hex_codes_in_unicode_sequences_enabled(context), normalize_hex: is_hex_codes_in_unicode_sequences_enabled(context),
format_fstring: is_f_string_formatting_enabled(context),
} }
} }
@ -156,7 +158,13 @@ impl StringNormalizer {
let quotes = self.choose_quotes(string, locator); let quotes = self.choose_quotes(string, locator);
let normalized = normalize_string(raw_content, quotes, string.prefix(), self.normalize_hex); let normalized = normalize_string(
raw_content,
quotes,
string.prefix(),
self.normalize_hex,
self.format_fstring,
);
NormalizedString { NormalizedString {
prefix: string.prefix(), prefix: string.prefix(),
@ -394,6 +402,7 @@ pub(crate) fn normalize_string(
quotes: StringQuotes, quotes: StringQuotes,
prefix: StringPrefix, prefix: StringPrefix,
normalize_hex: bool, normalize_hex: bool,
format_fstring: bool,
) -> Cow<str> { ) -> Cow<str> {
// The normalized string if `input` is not yet normalized. // The normalized string if `input` is not yet normalized.
// `output` must remain empty if `input` is already normalized. // `output` must remain empty if `input` is already normalized.
@ -409,7 +418,7 @@ pub(crate) fn normalize_string(
let mut chars = input.char_indices().peekable(); let mut chars = input.char_indices().peekable();
let is_raw = prefix.is_raw_string(); let is_raw = prefix.is_raw_string();
let is_fstring = prefix.is_fstring(); let is_fstring = !format_fstring && prefix.is_fstring();
let mut formatted_value_nesting = 0u32; let mut formatted_value_nesting = 0u32;
while let Some((index, c)) = chars.next() { while let Some((index, c)) = chars.next() {
@ -648,6 +657,7 @@ mod tests {
}, },
StringPrefix::BYTE, StringPrefix::BYTE,
true, true,
true,
); );
assert_eq!(r"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", &normalized); assert_eq!(r"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", &normalized);