Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
loic-simon opened this issue May 22, 2025 · 3 comments
Open

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

loic-simon opened this issue May 22, 2025 · 3 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@loic-simon
Copy link
Contributor

loic-simon commented May 22, 2025

Feature or enhancement

Proposal:

Update pprint functions to understand and format Template and Interpolation types:

>>> from pprint import pp
>>> name = "World"
>>> pp(t"Hello {World}")
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

@loic-simon loic-simon added the type-feature A feature request or enhancement label May 22, 2025
@emmatyping emmatyping added the stdlib Python modules in the Lib dir label May 23, 2025
@hugovk
Copy link
Member

hugovk commented May 23, 2025

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?

@loic-simon
Copy link
Contributor Author

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!

@lysnikolaou
Copy link
Member

lysnikolaou commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy