Update pprint functions to understand and format Template and Interpolation types:
>>> from pprint import pp
>>> name ="World"
>>> pp(t"Hello {name}")
Template(strings=('Hello ', ''),
interpolations=(Interpolation('World', 'name', None, ''),))""")
I believe it will be quite common to play with t-strings in the REPL and other dynamic environments, where pretty formatting could help a lot. I don't know how high is the bar for adding types to the pprint registry, though.
Unfortunately, "interpolation" being a quite long word repeated 2 times in the repr of a t-string, with default formatting rules we soon have text crumpled to the right of the screen... but I believe it is still more readable than
Activity
hugovk commented on May 23, 2025
Good idea, cc @davepeck @lysnikolaou
Or something like Black formatting takes up less vertical and horizontal space:
And taking the simpler case:
Formats like:
Or with --line-length 79 uses more vertical space but feels more readable:
Obviously we don't need to implement the full flexibility and complexity of Black-style formatting, but perhaps something along these lines?
loic-simon commented on May 23, 2025
I didn't dare to get away from pprint conventions (that follow PEP8, if I'm not mistaken) 😄 but I agree it would be way more readeable!
lysnikolaou commented on Jun 3, 2025
I agree with @hugovk that Black-like formatting makes this more readable. If people are okay with that, I'd certainly prefer it.
gh-134551: Add t-strings support to pprint (#134577)
pythongh-134551: Add t-strings support to pprint (python#134577)