[ty] Fix panic for unclosed string literal in type annotation position (#21592)

This commit is contained in:
Micha Reiser 2025-11-23 16:51:58 +01:00 committed by GitHub
parent d24c891a4b
commit aec225d825
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View File

@ -773,10 +773,14 @@ pub trait StringFlags: Copy {
} }
/// The total length of the string's closer. /// The total length of the string's closer.
/// This is always equal to `self.quote_len()`, /// This is always equal to `self.quote_len()`, except when the string is unclosed,
/// but is provided here for symmetry with the `opener_len()` method. /// in which case the length is zero.
fn closer_len(self) -> TextSize { fn closer_len(self) -> TextSize {
self.quote_len() if self.is_unclosed() {
TextSize::default()
} else {
self.quote_len()
}
} }
fn as_any_string_flags(self) -> AnyStringFlags { fn as_any_string_flags(self) -> AnyStringFlags {

View File

@ -715,3 +715,17 @@ def _(a: int, b: str, c: int | str):
x9: int | str | None = f(lst(c)) x9: int | str | None = f(lst(c))
reveal_type(x9) # revealed: int | str | None reveal_type(x9) # revealed: int | str | None
``` ```
## Forward annotation with unclosed string literal
Regression test for [#1611](https://github.com/astral-sh/ty/issues/1611).
<!-- blacken-docs:off -->
```py
# error: [invalid-syntax]
# error: [invalid-syntax-in-forward-annotation]
a:'
```
<!-- blacken-docs:on -->