mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
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:
parent
ad024f9a09
commit
9bbf4987e8
261 changed files with 18023 additions and 1802 deletions
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue