GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for Template type (t-strings) to pprint · Issue #134551 · python/cpython

Add support for Template type (t-strings) to pprint #134551

New issue
Copy link
New issue
Copy link
Closed
Closed
Add support for Template type (t-strings) to pprint#134551
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@loic-simon
loic-simon
opened on May 22, 2025
Issue body actions

Feature or enhancement

Proposal:

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.

If this is deemed worthwhile, I have a working implementation, pretty straightforward.


More complex examples (inspired from the PEP), to give a sense of what it could look like:

>>> attributes = {"id": "main"}
>>> attribute_value = "shrubbery"
>>> content = "hello"
>>> template = t"<span {attributes} data-value={attribute_value}>{content}</span>"
>>> pp(template)
Template(strings=('<span ', ' data-value=', '>', '</span>'),
         interpolations=(Interpolation({'id': 'main'}, 'attributes', None, ''),
                         Interpolation('shrubbery',
                                       'attribute_value',
                                       None,
                                       ''),
                         Interpolation('hello', 'content', None, '')))
>>>
>>> nested = t"<div>{template}</div>"
>>> pp(nested)
Template(strings=('<div>', '</div>'),
         interpolations=(Interpolation(Template(strings=('<span ',
                                                         ' data-value=',
                                                         '>',
                                                         '</span>'),
                                                interpolations=(Interpolation({'id': 'main'},
                                                                              'attributes',
                                                                              None,
                                                                              ''),
                                                                Interpolation('shrubbery',
                                                                              'attribute_value',
                                                                              None,
                                                                              ''),
                                                                Interpolation('hello',
                                                                              'content',
                                                                              None,
                                                                              ''))),
                                       'template',
                                       None,
                                       ''),))

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

Template(strings=('<div>', '</div>'), interpolations=(Interpolation(Template(strings=('<span ', ' da
ta-value=', '>', '</span>'), interpolations=(Interpolation({'id': 'main'}, 'attributes', None, ''),  
Interpolation('shrubbery', 'attribute_value', None, ''), Interpolation('hello', 'content', None, '')
)), 'template', None, ''),))

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

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

Activity

  1. loic-simon
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 22, 2025
  2. emmatyping
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 23, 2025
  3. hugovk commented on May 23, 2025

    @hugovk
    hugovk
    on May 23, 2025
    Member
    More actions

    Good idea, cc @davepeck @lysnikolaou


    More complex examples (inspired from the PEP), to give a sense of what it could look like:

    >>> attributes = {"id": "main"}
    >>> attribute_value = "shrubbery"
    >>> content = "hello"
    >>> template = t"<span {attributes} data-value={attribute_value}>{content}</span>"
    >>> pp(template)
    Template(strings=('<span ', ' data-value=', '>', '</span>'),
             interpolations=(Interpolation({'id': 'main'}, 'attributes', None, ''),
                             Interpolation('shrubbery',
                                           'attribute_value',
                                           None,
                                           ''),
                             Interpolation('hello', 'content', None, '')))
    >>>
    >>> nested = t"<div>{template}</div>"
    >>> pp(nested)
    Template(strings=('<div>', '</div>'),
             interpolations=(Interpolation(Template(strings=('<span ',
                                                             ' data-value=',
                                                             '>',
                                                             '</span>'),
                                                    interpolations=(Interpolation({'id': 'main'},
                                                                                  'attributes',
                                                                                  None,
                                                                                  ''),
                                                                    Interpolation('shrubbery',
                                                                                  'attribute_value',
                                                                                  None,
                                                                                  ''),
                                                                    Interpolation('hello',
                                                                                  'content',
                                                                                  None,
                                                                                  ''))),
                                           'template',
                                           None,
                                           ''),))

    Or something like Black formatting takes up less vertical and horizontal space:

    Template(
        strings=("<div>", "</div>"),
        interpolations=(
            Interpolation(
                Template(
                    strings=("<span ", " data-value=", ">", "</span>"),
                    interpolations=(
                        Interpolation({"id": "main"}, "attributes", None, ""),
                        Interpolation("shrubbery", "attribute_value", None, ""),
                        Interpolation("hello", "content", None, ""),
                    ),
                ),
                "template",
                None,
                "",
            ),
        ),
    )

    And taking the simpler case:

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

    Formats like:

    Template(
        strings=("Hello ", ""), interpolations=(Interpolation("World", "name", None, ""))
    )

    Or with --line-length 79 uses more vertical space but feels more readable:

    Template(
        strings=("Hello ", ""),
        interpolations=(Interpolation("World", "name", None, ""),),
    )

    Obviously we don't need to implement the full flexibility and complexity of Black-style formatting, but perhaps something along these lines?

  4. loic-simon commented on May 23, 2025

    @loic-simon
    loic-simon
    on May 23, 2025
    ContributorAuthor
    More actions

    Or something like Black formatting takes up less vertical and horizontal space:

    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!

  5. lysnikolaou commented on Jun 3, 2025

    @lysnikolaou
    lysnikolaou
    on Jun 3, 2025
    Member
    More actions

    I agree with @hugovk that Black-like formatting makes this more readable. If people are okay with that, I'd certainly prefer it.

    Reacted by Hugo van Kemenade
  6. hugovk
    added a commit that references this issue on Apr 14, 2026

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

    74a4f8c
  7. hugovk
    closed this as completedon Apr 14, 2026
  8. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-134551: Add t-strings support to pprint (python#134577)

    e3bf7d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.