Skip to content

gh-132661: Document t-strings and templatelib #135229

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
wants to merge 48 commits into
base: main
Choose a base branch
from

Conversation

davepeck
Copy link
Contributor

@davepeck davepeck commented Jun 6, 2025

This PR adds documentation for t-strings, including:

  • Document string.templatelib in the Library reference section
  • Add new t-string details to lexical-analysis.rst
  • Add AST nodes to the ast docs
  • Add BUILD_INTERPOLATION and BUILD_TEMPLATE to the dis docs
  • Update the PEP 292 section Template strings section of Doc/library/string.rst to clarify that this is unrelated to t-strings
  • Add a t-strings section to "fancier input/output formatting", after the f-strings section
  • Glossary

We also have an issue tracking our original list of PEP 750 documentation tasks.


📚 Documentation preview 📚: https://cpython-previews--135229.org.readthedocs.build/

@davepeck
Copy link
Contributor Author

@hugovk Thank you for the review and the helpful early feedback! Useful to know about linking.

(FWIW this is very early stage still, so I expect plenty more linting and other issues that we'll eventually resolve.)

@treyhunner
Copy link
Member

@davepeck The below thoughts are largely things that could be improved after this pull request is merged, if they are improved at all. The documentation currently in this pull request is well-worth merging in my opinion. I am looking forward to seeing this feature officially documented soon!

The standard library page

I noticed that string.templatelib is not linked from the Python standard library reference page but it is linked from the slightly deeper Text Processing Services page. I'm not sure whether this has already been discussed, but that seemed a bit confusing to me. I expected to find it on that page when I searched template and then I thought maybe it would be mentioned at the top of the string page instead. It took some clicking to realize it was accessible under "Text Processing Services".

image

Feedback on the string.templatelib page

In the "See also" box at the top my eyes read the links as one long link rather than 3 short links:

image

Putting commas between them or separating them over multiple lines might improve readability.

I could see myself reading the introduction at the top of the page and assuming that "t-strings return strings":

Template strings are an extension of f-strings that allow for greater control of formatting behavior. The Template class gives you access to the static and interpolated (in curly braces) parts of a string before they are combined into a final string.

If "extension of f-strings" was "extension of the f-string syntax" that might help a bit, but the end of the final sentence ("into a final string") might lead me to that conclusion most strongly. I would consider noting "Unlike f-strings, the t-string literal syntax creates a Template object" between those two sentences or somehow more strongly implying or noting that t-strings return Template objects rather than strings.

I like that the string.templatelib.Template starts off by noting that t-strings are the usual way to make a Template object. That fact seems easy to miss and it's great that it's mentioned first.

The Template and Interpolation attributes and methods look well-documented to me. One (possibly useful?) fact that seems to be missing from the documentation I've read so far is the fact that the parts/components/elements of a Template object will always alternate back and forth between str and Interpolation with one more string than interpolation (which also means they'll start and end with a string part).

Aside thought on "parts/components/elements": I'm not sure there's an official name for the "things you get when you loop over a Template". This isn't necessarily a problem for using t-strings, but would make explaining them easier. I often find myself unsure of how to teach concepts that don't have an official (or even semi-official) name.

Input and Output tutorial page

The "Template String Literals" section under the "Input and Output" page of the tutorial lines up nicely with the f-string section and explains the purpose fairly well while also being pretty succinct.

For the final bit about returning HTML elements, I found myself wanting more with this bit:

A full implementation of this function would be quite complex and is not provided here. That said, the fact that it is possible to implement a method like parse_html() showcases the flexibility and power of t-strings.

I would appreciate seeing a working example of a function that does not return a string.

It seems like maybe a different example should be used, or an additional full example of something shorter.

You're welcome to use a shorter various of this re.compile alternative that works with t-strings, though there may be better examples that are also relatively short.

@blaisep
Copy link
Contributor

blaisep commented Jul 9, 2025

... in heaven, everyone reviews code like @treyhunner

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this, it's looking good!

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you! I do have a bunch of nitpicks:

Comment on lines 180 to 183
.. _tut-t-strings:

Template String Literals
-------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please leave the tutorial to a separate PR (and replace tut-t-strings links with t-strings for now)?

This is a Diátaxis-style tutorial; IMO it would work better as an explanation and/or how-to guide.

Copy link
Contributor Author

@davepeck davepeck Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My instinct (and my read of review feedback so far) is that it feels like it fits well and follows nicely after the f-string section of the same doc (tutorial/inputoutput.rst).

Leaving this open while we consider our options here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll happily approve the rest of this PR.
For the tutorial I have more suggestions, so I'm asking to consider the options in a separate PR, to not block this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

@davepeck
Copy link
Contributor Author

davepeck commented Jul 10, 2025

@treyhunner Thank you for the amazing review!

I noticed that string.templatelib is not linked from the Python standard library reference page but it is linked from the slightly deeper Text Processing Services page

Fixed now!

In the "See also" box at the top my eyes read the links as one long link rather than 3 short links:

Fixed; thanks.

I could see myself reading the introduction at the top of the page and assuming that "t-strings return strings":

Agreed. I took another pass at the wording here. I still don't really love it; how does it work for you now?

One (possibly useful?) fact that seems to be missing from the documentation I've read so far is the fact that the parts/components/elements of a Template object will always alternate back and forth

It's in there but, yeah, it's scattered across description of Template's constructor, its .strings and .iterpolations properties, and iter(template).

The key property (which is included in the docs) is that there is always one more element in Template.strings than in Template.interpolations.

Related doc content:

  • When you construct a Template instance, you can pass str and Interpolation arguments in any order and the constructor will do the work under the hood to maintain the .strings/.interpolations count property (including inserting empty strings or concatenating adjacent strings as necessary)

  • When you iter(template) we skip empty strings (also described); if you want to grab the empty strings (say, for memoization) then you need to access .strings directly.

I hope this is okay? Earlier versions of the PEP explicitly mentioned alternation and made guarantees about it; the final form of the PEP mostly tries to avoid talking about it. If there's a way we can make these properties clearer sooner in the cpython docs, I'd love to find it — suggestions most welcome.

Aside thought on "parts/components/elements": I'm not sure there's an official name for the "things you get when you loop over a Template".

Hah! I've been going back and forth between "parts" and "items". You're absolutely right, we should settle on something here. I looked over the PEP, docs, and even some older discussions and "parts of a Template" seems to be the most common. I'll make updates where it makes sense.

I would appreciate seeing a working example of a function that does not return a string.

It seems like maybe a different example should be used, or an additional full example of something shorter.

You're welcome to use a shorter various of this re.compile alternative that works with t-strings, though there may be better examples that are also relatively short.

Thank you, this is useful feedback. I agree we should have something "complete" here. Will consider your cool re.compile example and circle back.

@encukou
Copy link
Member

encukou commented Jul 11, 2025

BTW, there's no need to update the PR if it's out of date with the main branch. GitHub just made the button for that too prominent.

@davepeck
Copy link
Contributor Author

BTW, there's no need to update the PR if it's out of date with the main branch. GitHub just made the button for that too prominent.

Hah, sorry; I know. It's sorta become a personal bad habit on PRs against active repos. I will refrain! 😅

davepeck and others added 5 commits July 12, 2025 09:31
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@davepeck
Copy link
Contributor Author

@treyhunner I just applied a suggestion from @encukou that I think nicely clarifies the alternation. Let us know what you think. (And: thanks @encukou !)

@davepeck
Copy link
Contributor Author

davepeck commented Jul 12, 2025

@encukou I removed the t-string tutorial for now. When I get a chance next week, I'll pop up a separate PR so we can wrangle it into shape. Onward! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

7 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