[ty] Highlight interpolated-parts in t-strings#22674
Merged
MichaReiser merged 4 commits intomainfrom Jan 18, 2026
Merged
Conversation
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.
AlexWaygood
approved these changes
Jan 18, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Related to astral-sh/ty#2539
Unifies the same code path to highlight t- and f-strings
Test Plan
Added tests