Python: Add f-string support for Python template/pattern API#6854
Merged
knutwannheden merged 3 commits intomainfrom Mar 3, 2026
Merged
Python: Add f-string support for Python template/pattern API#6854knutwannheden merged 3 commits intomainfrom
knutwannheden merged 3 commits intomainfrom
Conversation
Enable `template(f"print({expr})")` and `pattern(f"print({expr})")` as a
cleaner alternative to explicit kwargs like `template("print({expr})", expr=expr)`.
Works on all Python 3.6+ via `__format__` methods on Capture and RawCode:
- `Capture.__format__` returns `{name}` placeholder and auto-registers via contextvars
- `RawCode.__format__` splices code directly into the string
- `capture()` name parameter is now optional (auto-generated when omitted)
- `template()`/`pattern()` pick up auto-registered captures; explicit kwargs override
- Extract duplicated t-string/f-string/kwargs dispatch logic from template() and pattern() into resolve_captures() in _fstring_support.py - Replace _capture_counter global with itertools.count(1) - Fix inconsistent absolute imports in pattern.py to use relative imports - Remove unused _pending import from tests - Remove 6 redundant tests (30 -> 24): test_basic_format, test_empty_code, test_placeholder_output, test_multiple_captures, test_explicit_name_still_works, test_fstring_equivalent_to_kwargs
Have Capture.__format__, collect_captures, and convert_tstring produce
the internal __plh_name__ identifier directly instead of {name} braces.
This makes substitute_placeholders a no-op for f-string and t-string
paths, and avoids collision with Python's own brace syntax (dicts, sets).
Shorten the prefix from __placeholder_ to __plh_ for conciseness.
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
Add f-string and t-string support for the Python template/pattern API, enabling a more natural syntax for defining captures in templates.
Three ways to create templates with captures:
All three paths produce identical results.
RawCode(viaraw()) can be mixed with captures in f-strings and t-strings for splice-time code insertion (e.g. dynamic method names).Implementation
Capture.__format__emits the internal placeholder identifier (__plh_name__) directly, making f-string code already in the engine's internal format —substitute_placeholdersbecomes a no-opconvert_tstringdoes the same for t-stringscontextvars.ContextVarprevents interference between concurrent f-string evaluationsresolve_captures()helper dispatches t-string / f-string / explicit-kwargs uniformly for bothtemplate()andpattern()Test plan
test_capture.py—__format__output, auto-registration, stale cleanup, kwargs overridetest_tstring_support.py— t-string conversion with captures, raw code, mixed usagetest_placeholder.py— placeholder conversion, substitution, validationtest_engine.py— parsing with placeholders, caching, auto-format integrationtest_replacement.py— placeholder replacement, auto-parenthesization