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

@ -78,6 +78,7 @@ impl From<&Expr> for ResolvedPythonType {
Expr::Tuple(_) => ResolvedPythonType::Atom(PythonType::Tuple),
Expr::Generator(_) => ResolvedPythonType::Atom(PythonType::Generator),
Expr::FString(_) => ResolvedPythonType::Atom(PythonType::String),
Expr::TString(_) => ResolvedPythonType::Unknown,
Expr::StringLiteral(_) => ResolvedPythonType::Atom(PythonType::String),
Expr::BytesLiteral(_) => ResolvedPythonType::Atom(PythonType::Bytes),
Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value {

View file

@ -1939,10 +1939,15 @@ impl<'a> SemanticModel<'a> {
self.flags.intersects(SemanticModelFlags::F_STRING)
}
/// Return `true` if the model is in a t-string.
pub const fn in_t_string(&self) -> bool {
self.flags.intersects(SemanticModelFlags::T_STRING)
}
/// Return `true` if the model is in an f-string replacement field.
pub const fn in_f_string_replacement_field(&self) -> bool {
pub const fn in_interpolated_string_replacement_field(&self) -> bool {
self.flags
.intersects(SemanticModelFlags::F_STRING_REPLACEMENT_FIELD)
.intersects(SemanticModelFlags::INTERPOLATED_STRING_REPLACEMENT_FIELD)
}
/// Return `true` if the model is in boolean test.
@ -2461,7 +2466,7 @@ bitflags! {
/// ```python
/// f"first {x} second {y}"
/// ```
const F_STRING_REPLACEMENT_FIELD = 1 << 21;
const INTERPOLATED_STRING_REPLACEMENT_FIELD = 1 << 21;
/// The model is visiting the bases tuple of a class.
///
@ -2549,6 +2554,15 @@ bitflags! {
/// [#13824]: https://github.com/astral-sh/ruff/issues/13824
const NO_TYPE_CHECK = 1 << 28;
/// The model is in a t-string.
///
/// For example, the model could be visiting `x` in:
/// ```python
/// t'{x}'
/// ```
const T_STRING = 1 << 29;
/// The context is in any type annotation.
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();