Skip to content

invalid-syntax-in-forward-annotation does not preserve error spans #1627

@MeGaGiGaGon

Description

@MeGaGiGaGon

Summary

As the title says, invalid-syntax-in-forward-annotation does not preserve error spans. For example:
https://play.ty.dev/f034109f-c63d-4871-9976-dbea46491a62

a: type]
b: "type]"

The span of a's error is 1:8-1:9 (well actually it has two errors, but both are on the ]).
The span of b's error is the entire string literal. It would be nice if ty preserved the invalid-syntax error location and propagated it to invalid-syntax-in-forward-annotation

PS ~>Set-Content issue.py @'
a: type]
b: "type]"
'@
PS ~>uvx ty check issue.py
error[invalid-syntax]: Expected a statement
 --> issue.py:1:8
  |
1 | a: type]
  |        ^
2 | b: "type]"
  |

error[invalid-syntax]: Expected a statement
 --> issue.py:1:9
  |
1 | a: type]
  |         ^
2 | b: "type]"
  |

error[invalid-syntax-in-forward-annotation]: Syntax error in forward annotation: Unexpected token at the end of an expression
 --> issue.py:2:4
  |
1 | a: type]
2 | b: "type]"
  |    ^^^^^^^
  |
info: rule `invalid-syntax-in-forward-annotation` is enabled by default

Found 3 diagnostics

The error message is also not always helpful, since the error may not occur at the end, ie type] | int

Version

ty 0.0.1-alpha.27 (26d7b68 2025-11-18)

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions