Skip to content

[ty] Highlight interpolated-parts in t-strings#22674

Merged
MichaReiser merged 4 commits intomainfrom
claude/investigate-issue-2539-iVgL5
Jan 18, 2026
Merged

[ty] Highlight interpolated-parts in t-strings#22674
MichaReiser merged 4 commits intomainfrom
claude/investigate-issue-2539-iVgL5

Conversation

@MichaReiser
Copy link
Member

Summary

Related to astral-sh/ty#2539

Unifies the same code path to highlight t- and f-strings

Test Plan

Added tests

The semantic tokens visitor was missing a `visit_t_string` method,
which meant that t-strings (Python 3.14 template strings) were not
receiving proper semantic token classification. While the default
visitor would still walk through t-string expressions, it would not:

1. Emit `String` tokens for literal parts of t-strings
2. Properly handle format specs within t-string interpolations

This commit adds a `visit_t_string` method that mirrors the existing
`visit_f_string` implementation, ensuring t-strings receive the same
semantic highlighting treatment as f-strings.

Fixes part of astral-sh/ty#2539
Extract the duplicated interpolated string element handling into a
shared helper method `visit_interpolated_string_elements`. This reduces
code duplication and ensures consistent behavior between f-strings and
t-strings.

The helper method also recursively handles format specs, which
simplifies the implementation.
… string visitors

Instead of overriding both `visit_f_string` and `visit_t_string`, override
the single `visit_interpolated_string_element` method. This is cleaner
because:

1. Only one override is needed instead of two
2. The default `visit_f_string` and `visit_t_string` call
   `visit_interpolated_string_element` automatically
3. Any future interpolated string types will also work automatically
The default walker already handles visiting the expression and format
spec correctly, so we only need to handle the Literal case ourselves
to emit the String token.
@MichaReiser MichaReiser added bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference labels Jan 18, 2026
@MichaReiser MichaReiser changed the title [ty] Highlight string-parts in t-strings [ty] Highlight interpolated-parts in t-strings Jan 18, 2026
@MichaReiser MichaReiser merged commit 0e5bac0 into main Jan 18, 2026
46 checks passed
@MichaReiser MichaReiser deleted the claude/investigate-issue-2539-iVgL5 branch January 18, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants