[ty] Improve diagnostics for syntax errors in forward annotations by AlexWaygood · Pull Request #25158 · astral-sh/ruff (original) (raw)

AlexWaygood

MichaReiser

@AlexWaygood

@carljm carljm removed their request for review

May 27, 2026 21:09

MichaReiser

@AlexWaygood @MichaReiser

Co-authored-by: Micha Reiser micha@reiser.io

anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request

May 28, 2026

…tral-sh#25158)

Summary

Fixes astral-sh/ty#1627.

Here's an example diagnostic with the current release of ty:

image

On this branch, this diagnostic becomes:

image

The exact span of the node that creates the syntax error is now retained and highlighted in the diagnostic.

Implementation

Propagating the range of the node inside the string annotation into the diagnostic is trivial. However, naively implementing that quickly revealed that this would make diagnostics like this unsuppressable:

x: """list[
    yield from range(42)
]"""

The primary range of the diagnostic is now specifically the yield from range(42) part of the string rather than the string node as a whole. But I cannot add a ty: ignore comment that is either on or above the yield from range(42) part of the string -- the "comment" there would just become part of the string.

This PR also improves the consistency of our parser error messages in general when it comes to capitalization.

Test Plan

Mdtests extended and updated


Co-authored-by: Micha Reiser micha@reiser.io

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})