Skip to content

gh-134551: Add t-strings support to pprint - #134577

Merged
hugovk merged 9 commits into
python:mainfrom
loic-simon:add-templates-to-pprint
Apr 14, 2026
Merged

hugovk merged 9 commits into
python:mainfrom
loic-simon:add-templates-to-pprint

Conversation

@loic-simon

@loic-simon loic-simon commented May 23, 2025 •

Copy link
Copy Markdown
Contributor

Handle formatting of Template and Interpolation types in pprint functions:

>>> from pprint import pp
>>> name = "World"
>>> pp(t"Hello {name}")
Template(strings=('Hello ', ''),
         interpolations=(Interpolation('World', 'name', None, ''),))""")

Very straightforward, using existing PrettyPrinter helper methods.

To avoid slowing import time, I did not import Template and Interpolation from string.templatelib, but instead copied their (very simple) definition:

t = t"{0}"
Template = type(t)
Interpolation = type(t.interpolations[0])
del t

@loic-simon

loic-simon commented May 23, 2025 •

Copy link
Copy Markdown
Contributor Author

Not sure why test.test_tokenize.TestRoundtrip.test_random_files fails on test_pprint.py? If I replace t-strings by f-strings it pass, is it an issue with the test not handling t-strings? Or a real issue with tokenizer round-trippring and t-strings?

Edit: looks like a real problem, I have a MRE, opening an issue soon

Comment thread Lib/test/.ruff.toml Resolved
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@AlexWaygood
AlexWaygood removed their request for review June 16, 2025 22:36
@hugovk

hugovk commented Apr 13, 2026 •

Copy link
Copy Markdown
Member

I've updated this PR to fix the merge conflict, add support for the new expand=True from #136964, and add this to What's New.

>>> from pprint import pp
>>> name = "World"
>>> pp(t"Hello {name}")
Template(strings=('Hello ', ''),
         interpolations=(Interpolation('World', 'name', None, ''),))
>>> pp(t"Hello {name}", expand=True)
Template(
 strings=('Hello ', ''),
 interpolations=(Interpolation('World', 'name', None, ''),),
)
@hugovk
hugovk merged commit 74a4f8c into python:main Apr 14, 2026
51 checks passed
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants