[ty] Respect debug text interpolation in f-strings (#22151)

## Summary

Per @carljm's comment, we just fall back to `str`.

Closes https://github.com/astral-sh/ty/issues/2151.
This commit is contained in:
Charlie Marsh
2025-12-22 20:21:28 -05:00
committed by GitHub
parent 06db474f20
commit 4745d15fff
2 changed files with 15 additions and 2 deletions

View File

@@ -24,6 +24,16 @@ string = "hello"
reveal_type(f"{string!r}") # revealed: str
```
## Debug Specifier
The `=` specifier causes the expression text and value to be included in the output:
```py
# f"{1=}" evaluates to "1=1", but we fall back to `str` for now
reveal_type(f"{1=}") # revealed: str
reveal_type(f"value: {42=}") # revealed: str
```
## Format Specifiers
```py

View File

@@ -7594,7 +7594,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
range: _,
node_index: _,
expression,
debug_text: _,
debug_text,
conversion,
format_spec,
} = expression;
@@ -7613,7 +7613,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
// (`Type::format`?) that handles the `__format__` method.
// Conversion flags should be handled before calling `__format__`.
// https://docs.python.org/3/library/string.html#format-string-syntax
if !conversion.is_none() || format_spec.is_some() {
if debug_text.is_some()
|| !conversion.is_none()
|| format_spec.is_some()
{
collector.add_expression();
} else {
if let Type::StringLiteral(literal) = ty.str(self.db()) {