diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index b57b86aaf2..2ea2701f82 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -773,10 +773,14 @@ pub trait StringFlags: Copy { } /// The total length of the string's closer. - /// This is always equal to `self.quote_len()`, - /// but is provided here for symmetry with the `opener_len()` method. + /// This is always equal to `self.quote_len()`, except when the string is unclosed, + /// in which case the length is zero. 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 { diff --git a/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md b/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md index 9471f4bc82..3baf76dce3 100644 --- a/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md +++ b/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md @@ -715,3 +715,17 @@ def _(a: int, b: str, c: int | str): x9: int | str | None = f(lst(c)) 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). + + + +```py +# error: [invalid-syntax] +# error: [invalid-syntax-in-forward-annotation] +a:' +``` + +