Rename ExprStringLiteral::as_unconcatenated_string() to ExprStringLiteral::as_single_part_string() (#16253)

This commit is contained in:
Alex Waygood 2025-02-19 16:06:57 +00:00 committed by GitHub
parent 97d0659ce3
commit 25920fe489
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 50 additions and 45 deletions

View file

@ -13,9 +13,7 @@ pub struct FormatExprBytesLiteral;
impl FormatNodeRule<ExprBytesLiteral> for FormatExprBytesLiteral {
fn fmt_fields(&self, item: &ExprBytesLiteral, f: &mut PyFormatter) -> FormatResult<()> {
let ExprBytesLiteral { value, .. } = item;
if let [bytes_literal] = value.as_slice() {
if let Some(bytes_literal) = item.as_single_part_bytestring() {
bytes_literal.format().fmt(f)
} else {
// Always join byte literals that aren't parenthesized and thus, always on a single line.

View file

@ -15,13 +15,7 @@ pub struct FormatExprFString;
impl FormatNodeRule<ExprFString> for FormatExprFString {
fn fmt_fields(&self, item: &ExprFString, f: &mut PyFormatter) -> FormatResult<()> {
let ExprFString { value, .. } = item;
if let [f_string_part] = value.as_slice() {
// 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();
if let Some(f_string) = item.as_single_part_fstring() {
f_string.format().fmt(f)
} else {
// Always join fstrings that aren't parenthesized and thus, are always on a single line.
@ -44,16 +38,18 @@ impl NeedsParentheses for ExprFString {
_parent: AnyNodeRef,
context: &PyFormatContext,
) -> OptionalParentheses {
if self.value.is_implicit_concatenated() {
OptionalParentheses::Multiline
} else if StringLike::FString(self).is_multiline(context)
|| self.value.as_single().is_some_and(|f_string| {
FStringLayout::from_f_string(f_string, context.source()).is_multiline()
})
{
OptionalParentheses::Never
if let Some(fstring_part) = self.as_single_part_fstring() {
// The f-string is not implicitly concatenated
if StringLike::FString(self).is_multiline(context)
|| FStringLayout::from_f_string(fstring_part, context.source()).is_multiline()
{
OptionalParentheses::Never
} else {
OptionalParentheses::BestFit
}
} else {
OptionalParentheses::BestFit
// The f-string is implicitly concatenated
OptionalParentheses::Multiline
}
}
}

View file

@ -28,7 +28,7 @@ impl FormatRuleWithOptions<ExprStringLiteral, PyFormatContext<'_>> for FormatExp
impl FormatNodeRule<ExprStringLiteral> for FormatExprStringLiteral {
fn fmt_fields(&self, item: &ExprStringLiteral, f: &mut PyFormatter) -> FormatResult<()> {
if let Some(string_literal) = item.as_unconcatenated_literal() {
if let Some(string_literal) = item.as_single_part_string() {
string_literal.format().with_options(self.kind).fmt(f)
} else {
// Always join strings that aren't parenthesized and thus, always on a single line.

View file

@ -1,7 +1,7 @@
use ruff_formatter::{format_args, write, FormatError, RemoveSoftLinesBuffer};
use ruff_python_ast::{
AnyNodeRef, Expr, ExprAttribute, ExprCall, FString, FStringPart, Operator, StmtAssign,
StringLike, TypeParams,
AnyNodeRef, Expr, ExprAttribute, ExprCall, FString, Operator, StmtAssign, StringLike,
TypeParams,
};
use crate::builders::parenthesize_if_expands;
@ -1107,9 +1107,7 @@ fn format_f_string_assignment<'a>(
return None;
};
let [FStringPart::FString(f_string)] = expr.value.as_slice() else {
return None;
};
let f_string = expr.as_single_part_fstring()?;
// If the f-string is flat, there are no breakpoints from which it can be made multiline.
// This is the case when the f-string has no expressions or if it does then the expressions