PEP 750 tstring by youknowone · Pull Request #6744 · RustPython/RustPython
📝 Walkthrough
Walkthrough
Adds full support for template strings (t-strings / PEP 750) end-to-end: AST representation, codegen emitting new bytecode ops, new bytecode instructions, VM execution handlers, and runtime types PyInterpolation and PyTemplate.
Changes
| Cohort / File(s) | Summary |
|---|---|
Code generation crates/codegen/src/compile.rs |
Implemented compile_expr_tstring and compile_tstring_into, replacing NotImplemented paths to emit BuildInterpolation and BuildTemplate. |
Symbol scanning crates/codegen/src/symboltable.rs |
Scan t-string interpolation expressions for symbols (mirrors f-string logic) instead of erroring. |
Bytecode / Instruction set crates/compiler-core/src/bytecode/instruction.rs |
Added BuildTemplate (opcode 125) and BuildInterpolation { oparg } (opcode 126); updated decoding, metadata (stack effects), and disassembly formatting. |
VM frame / execution crates/vm/src/frame.rs |
Implemented handlers for BuildInterpolation (construct PyInterpolation from stack, decode oparg) and BuildTemplate (build PyTemplate from tuples). |
Runtime types — Interpolation crates/vm/src/builtins/interpolation.rs |
New PyInterpolation type exposed as string.templatelib.Interpolation with constructor, validation, equality, hashing, repr, pickling, getters, and registration. |
Runtime types — Template crates/vm/src/builtins/template.rs |
New PyTemplate and PyTemplateIter types with construction/validation, concatenation, iteration, equality, repr, pickling, and getters; registered in builtins. |
Builtins exports crates/vm/src/builtins/mod.rs |
Added modules and re-exports for interpolation and template (PyInterpolation, PyTemplate, PyTemplateIter). |
AST integration crates/vm/src/stdlib/ast/string.rs, crates/vm/src/stdlib/ast/expression.rs, crates/vm/src/stdlib/ast/pyast.rs |
Added tstring_to_object, TemplateStr/TemplateStrPart/TStringInterpolation nodes and conversions; registered new AST node classes TemplateStr and Interpolation; replaced previous TString unimplemented flows. |
Type zoo crates/vm/src/types/zoo.rs |
Added interpolation_type, template_type, and template_iter_type fields and init wiring. |
VM ops (minor) crates/vm/src/vm/vm_ops.rs |
Refactored concat/add dispatch to prefer direct slot-based concat/inplace-slot paths over try_sequence. |
Sequence Diagram(s)
sequenceDiagram
participant Parser
participant Codegen as Code Generator
participant Bytecode
participant VM as Virtual Machine
participant Runtime as Builtins
Parser->>Codegen: Emit TString AST node
Codegen->>Codegen: compile_expr_tstring()
Codegen->>Codegen: collect literal parts and interpolations
Codegen->>Bytecode: emit BuildInterpolation (per interpolation)
Codegen->>Bytecode: emit BuildTemplate
VM->>VM: execute BuildInterpolation
VM->>Runtime: construct PyInterpolation(value, expr, conversion, format_spec)
Runtime-->>VM: PyInterpolation object
VM->>VM: execute BuildTemplate
VM->>Runtime: construct PyTemplate(strings_tuple, interpolations_tuple)
Runtime-->>VM: PyTemplate object
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~50 minutes
Possibly related PRs
- Sort
Instructionenum & related match arms #6322: Related changes to Instruction enum/opcode handling in compiler-core; overlaps with new opcodes here. - Bytecode instrumented placeholder #6741: Modifies bytecode instruction definitions/metadata similar to the new custom opcodes added in this PR.
- Rework compiler about exception handling #6638: Also adjusts the bytecode instruction set and encoding; likely related to integrating new instructions.
Suggested reviewers
- ShaharNaveh
Poem
🐇
I stitched the strings with hops and cheer,
Interpolations snug and near,
BuildInterpolation, BuildTemplate too,
PyTemplate blooms — a rabbit’s view,
T-strings prance, all bright and clear.
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 38.60% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'PEP 750 tstring' clearly summarizes the main change: implementing PEP 750 template string support, as evidenced by widespread tstring/template-related additions across compilation, VM, AST, and builtins modules. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.