Add inference for t-strings#3120
Draft
Pierre-Sassoulas wants to merge 2 commits into
Draft
Conversation
A t-string is now inferred as an instance of string.templatelib.Template, and an interpolation field as an instance of string.templatelib.Interpolation, rather than raising or inferring as str (unlike f-strings / JoinedStr). The Template/Interpolation members are reconstructed from the actual template contents, mirroring the PEP 750 runtime semantics: Template.strings/values/interpolations and Interpolation.value/expression/conversion/format_spec infer to the same values produced at runtime. This is implemented with dedicated TemplateStrInstance / InterpolationInstance objects backed by TemplateStrModel / InterpolationModel object models, in the same way DictInstance/ExceptionInstance expose synthetic members. The standard library defines Template and Interpolation via type(t"..."), which is not statically inferable. A new brain plugin (brain_string) rebuilds the real classes from the live objects via InspectBuilder, so they resolve to ClassDef nodes with the genuine PEP 750 API instead of a hand-written stub. Also fix as_string for TemplateStr to double literal braces and use repr-based escaping in the constant parts, mirroring JoinedStr, so the rendered code round-trips. Closes #2789
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3120 +/- ##
==========================================
+ Coverage 93.60% 93.68% +0.08%
==========================================
Files 92 93 +1
Lines 11381 11496 +115
==========================================
+ Hits 10653 10770 +117
+ Misses 728 726 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
The generator pauses at yield util.Uninferable, so the tests exhaust it with list() to also execute the return lines. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Type of Changes
Description
A t-string is now inferred as an instance of
string.templatelib.Template, and an interpolation field as an instance of string.templatelib.Interpolation, rather than raising or inferring as str (unlike f-strings / JoinedStr).
The Template/Interpolation members are reconstructed from the actual template contents, mirroring the PEP 750 runtime semantics: Template.strings/values/interpolations and
Interpolation.value/expression/conversion/format_spec infer to the same values produced at runtime. This is implemented with dedicated TemplateStrInstance / InterpolationInstance objects backed by TemplateStrModel / InterpolationModel object models, in the same way DictInstance/ExceptionInstance expose synthetic members.
The standard library defines Template and Interpolation via type(t"..."), which is not statically inferable. A new brain plugin (brain_string) rebuilds the real classes from the live objects via InspectBuilder, so they resolve to ClassDef nodes with the genuine PEP 750 API instead of a hand-written stub.
Also fix as_string for TemplateStr to double literal braces and use repr-based escaping in the constant parts, mirroring JoinedStr, so the rendered code round-trips.
Closes #2789