Remove FormatFStringPart (#14448)

## Summary

This is just a small refactor to remove the `FormatFStringPart` as it's
only used in the case when the f-string is not implicitly concatenated
in which case the only part is going to be `FString`. In implicitly
concatenated f-strings, we use `StringLike` instead.
This commit is contained in:
Dhruv Manilawala 2024-11-25 10:29:22 +05:30 committed by GitHub
parent ac23c99744
commit efe54081d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 6 additions and 42 deletions

View file

@ -4,7 +4,7 @@ use ruff_text_size::TextSlice;
use crate::expression::parentheses::{
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
};
use crate::other::f_string_part::FormatFStringPart;
use crate::other::f_string::FormatFString;
use crate::prelude::*;
use crate::string::implicit::FormatImplicitConcatenatedStringFlat;
use crate::string::{implicit::FormatImplicitConcatenatedString, Quoting, StringLikeExtensions};
@ -17,8 +17,11 @@ impl FormatNodeRule<ExprFString> for FormatExprFString {
let ExprFString { value, .. } = item;
if let [f_string_part] = value.as_slice() {
FormatFStringPart::new(f_string_part, f_string_quoting(item, f.context().source()))
.fmt(f)
// SAFETY: A single string literal cannot be an f-string. This is guaranteed by the
// [`ruff_python_ast::FStringValue::single`] constructor.
let f_string = f_string_part.as_f_string().unwrap();
FormatFString::new(f_string, f_string_quoting(item, f.context().source())).fmt(f)
} else {
// Always join fstrings that aren't parenthesized and thus, are always on a single line.
if !f.context().node_level().is_parenthesized() {

View file

@ -1,38 +0,0 @@
use ruff_python_ast::FStringPart;
use crate::other::f_string::FormatFString;
use crate::other::string_literal::StringLiteralKind;
use crate::prelude::*;
use crate::string::Quoting;
/// Formats an f-string part which is either a string literal or an f-string.
///
/// This delegates the actual formatting to the appropriate formatter.
pub(crate) struct FormatFStringPart<'a> {
part: &'a FStringPart,
/// The quoting to be used for all the f-string parts. This is determined by
/// the parent node (f-string expression) and is required to format all parts
/// correctly.
quoting: Quoting,
}
impl<'a> FormatFStringPart<'a> {
pub(crate) fn new(part: &'a FStringPart, quoting: Quoting) -> Self {
Self { part, quoting }
}
}
impl Format<PyFormatContext<'_>> for FormatFStringPart<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
match self.part {
#[allow(deprecated)]
FStringPart::Literal(string_literal) => string_literal
.format()
.with_options(StringLiteralKind::InImplicitlyConcatenatedFString(
self.quoting,
))
.fmt(f),
FStringPart::FString(f_string) => FormatFString::new(f_string, self.quoting).fmt(f),
}
}
}

View file

@ -8,7 +8,6 @@ pub(crate) mod elif_else_clause;
pub(crate) mod except_handler_except_handler;
pub(crate) mod f_string;
pub(crate) mod f_string_element;
pub(crate) mod f_string_part;
pub(crate) mod identifier;
pub(crate) mod keyword;
pub(crate) mod match_case;