Implement template strings (#17851)

This PR implements template strings (t-strings) in the parser and
formatter for Ruff.

Minimal changes necessary to compile were made in other parts of the code (e.g. ty, the linter, etc.). These will be covered properly in follow-up PRs.
This commit is contained in:
Dylan 2025-05-30 15:00:56 -05:00 committed by GitHub
parent ad024f9a09
commit 9bbf4987e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
261 changed files with 18023 additions and 1802 deletions

View file

@ -3,7 +3,7 @@ use ruff_python_ast::{AnyNodeRef, ExprFString, StringLike};
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
};
use crate::other::f_string::FStringLayout;
use crate::other::interpolated_string::InterpolatedStringLayout;
use crate::prelude::*;
use crate::string::StringLikeExtensions;
use crate::string::implicit::{
@ -41,7 +41,11 @@ impl NeedsParentheses for ExprFString {
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()
|| InterpolatedStringLayout::from_interpolated_string_elements(
&fstring_part.elements,
context.source(),
)
.is_multiline()
{
OptionalParentheses::Never
} else {

View file

@ -0,0 +1,59 @@
use ruff_python_ast::{AnyNodeRef, ExprTString, StringLike};
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, in_parentheses_only_group,
};
use crate::other::interpolated_string::InterpolatedStringLayout;
use crate::prelude::*;
use crate::string::StringLikeExtensions;
use crate::string::implicit::{
FormatImplicitConcatenatedString, FormatImplicitConcatenatedStringFlat,
};
#[derive(Default)]
pub struct FormatExprTString;
impl FormatNodeRule<ExprTString> for FormatExprTString {
fn fmt_fields(&self, item: &ExprTString, f: &mut PyFormatter) -> FormatResult<()> {
if let Some(t_string) = item.as_single_part_tstring() {
t_string.format().fmt(f)
} else {
// Always join tstrings that aren't parenthesized and thus, are always on a single line.
if !f.context().node_level().is_parenthesized() {
if let Some(format_flat) =
FormatImplicitConcatenatedStringFlat::new(item.into(), f.context())
{
return format_flat.fmt(f);
}
}
in_parentheses_only_group(&FormatImplicitConcatenatedString::new(item)).fmt(f)
}
}
}
impl NeedsParentheses for ExprTString {
fn needs_parentheses(
&self,
_parent: AnyNodeRef,
context: &PyFormatContext,
) -> OptionalParentheses {
if let Some(tstring_part) = self.as_single_part_tstring() {
// The t-string is not implicitly concatenated
if StringLike::TString(self).is_multiline(context)
|| InterpolatedStringLayout::from_interpolated_string_elements(
&tstring_part.elements,
context.source(),
)
.is_multiline()
{
OptionalParentheses::Never
} else {
OptionalParentheses::BestFit
}
} else {
// The t-string is implicitly concatenated
OptionalParentheses::Multiline
}
}
}

View file

@ -50,6 +50,7 @@ pub(crate) mod expr_slice;
pub(crate) mod expr_starred;
pub(crate) mod expr_string_literal;
pub(crate) mod expr_subscript;
pub(crate) mod expr_t_string;
pub(crate) mod expr_tuple;
pub(crate) mod expr_unary_op;
pub(crate) mod expr_yield;
@ -94,6 +95,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
Expr::Compare(expr) => expr.format().fmt(f),
Expr::Call(expr) => expr.format().fmt(f),
Expr::FString(expr) => expr.format().fmt(f),
Expr::TString(expr) => expr.format().fmt(f),
Expr::StringLiteral(expr) => expr.format().fmt(f),
Expr::BytesLiteral(expr) => expr.format().fmt(f),
Expr::NumberLiteral(expr) => expr.format().fmt(f),
@ -282,6 +284,7 @@ fn format_with_parentheses_comments(
Expr::Compare(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Call(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::FString(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::TString(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::StringLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::BytesLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::NumberLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
@ -480,6 +483,7 @@ impl NeedsParentheses for Expr {
Expr::Compare(expr) => expr.needs_parentheses(parent, context),
Expr::Call(expr) => expr.needs_parentheses(parent, context),
Expr::FString(expr) => expr.needs_parentheses(parent, context),
Expr::TString(expr) => expr.needs_parentheses(parent, context),
Expr::StringLiteral(expr) => expr.needs_parentheses(parent, context),
Expr::BytesLiteral(expr) => expr.needs_parentheses(parent, context),
Expr::NumberLiteral(expr) => expr.needs_parentheses(parent, context),
@ -775,6 +779,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
// Terminal nodes or nodes that wrap a sub-expression (where the sub expression can never be at the end).
Expr::FString(_)
| Expr::TString(_)
| Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
@ -1126,6 +1131,7 @@ pub(crate) fn is_expression_huggable(expr: &Expr, context: &PyFormatContext) ->
| Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::FString(_)
| Expr::TString(_)
| Expr::EllipsisLiteral(_) => false,
}
}
@ -1221,6 +1227,7 @@ pub(crate) fn is_splittable_expression(expr: &Expr, context: &PyFormatContext) -
// String like literals can expand if they are implicit concatenated.
Expr::FString(fstring) => fstring.value.is_implicit_concatenated(),
Expr::TString(tstring) => tstring.value.is_implicit_concatenated(),
Expr::StringLiteral(string) => string.value.is_implicit_concatenated(),
Expr::BytesLiteral(bytes) => bytes.value.is_implicit_concatenated(),
@ -1278,6 +1285,7 @@ pub(crate) fn left_most<'expr>(
| Expr::Name(_)
| Expr::Starred(_)
| Expr::FString(_)
| Expr::TString(_)
| Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)