Latest / Talk Python To Me / #505: t-strings in Python (PEP 750)
Transcript
- 0:00Python has many string formatting styles, which have been added to the language over the years.
- 0:04Early Python used the percent operator to inject formatted values into strings, and we have string.format, which offers several powerful styles.
- 0:14Both were verbose and indirect, so fstrings were added in Python 3.6, but these fstrings lacked security features.
- 0:21Think about little bobby tables.
- 0:22And they manifested as fully formed strings to run timecode.
- 0:26Today, we talk about the next evolution of Python string formatting for advanced use cases.
- 0:32Think SQL, HTML, DSLs, and so on.
- 0:35t-strings.
- 0:37We have Paul Everitt, Dave Peck, and Jim Baker on the show to introduce the upcoming language feature.
- 0:43This is Talk Python To Me, episode 505, recorded May 13th, 2025.
- 0:49Are
- 0:49you ready for your host, please?
- 0:52You're listening to Michael Kennedy on Talk Python To Me.
- 0:55Live from Portland, Oregon, and this segment was made with Python.
- 1:24Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified about upcoming shows.
- 1:31This episode is sponsored by Posit Connect from the makers of Shiny.
- 1:35Publish, share, and deploy all of your data projects that you're creating using Python.
- 1:40Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, Reports, Dashboards, and APIs.
- 1:47Posit Connect supports all of them.
- 1:48Try Posit Connect for free by going to talkpython.fm/Posit, P-O-S-I-T.
- 1:55And it's brought to you by Auth0.
- 1:57Auth0 is an easy-to-implement, adaptable authentication and authorization platform.
- 2:02Think easy user logins, social sign-on, multi-factor authentication, and robust role-based access control.
- 2:09With over 30 SDKs and quick starts, Auth0 scales with your product at every stage.
- 2:15Get 25,000 monthly active users for free at talkpython.fm/auth0.
- 2:22Dave, Jim, and Paul, welcome to all three of you to Talk Python To Me.
- 2:26Paul, welcome back.
- 2:27Dave and Jim, welcome for the first time.
- 2:29Awesome to have you here.
- 2:30Thanks.
- 2:31Thanks.
- 2:31We are going to carry on an amazing tradition of Python string formatting.
- 2:38We've had the percent.
- 2:40We've had the plus.
- 2:41We've had the dot format.
- 2:44We've had the dot join.
- 2:45Everyone was very excited about fstrings back in 3.6 when they came out.
- 2:49And now, after quite a bit of work from the three of you and others, we're going to be talking about PEP 750 t-strings.
- 2:58Amazing.
- 2:59So congratulations on all of that.
- 3:01I think it's going to be a lot of fun.
- 3:02People may be surprised to think that, oh, there's an entire episode with three whole people on string formatting.
- 3:10Yes.
- 3:10Yes, that's right.
- 3:11There's actually a lot to it.
- 3:12This is a pretty rich API and a pretty big advancement.
- 3:16So we're going to dive into that.
- 3:18But before we do, how about a round of introductions?
- 3:21Dave?
- 3:21Hey, I'm Dave Peck.
- 3:23I'm a freelance software developer based in sunny Seattle, Washington.
- 3:28A freelance software developer is a polite way of saying I'm a computer weirdo.
- 3:32And I get to get into hopefully all kinds of good computer mischief, like working with Jim and Paul on new peps for Python.
- 3:39Awesome.
- 3:39Great to have you here.
- 3:40And always good to have another person from the Pacific Northwest.
- 3:44Jim, welcome.
- 3:45Hi.
- 3:46So, yeah, I'm a longtime Python user.
- 3:50I used to be very actively involved in the Jython project, which let me really learn the internals of both Jython and CPython, since we had to maintain a compatible implementation.
- 4:01So, you know, got deep into things like compilers and the standard library.
- 4:07And, yeah, it's great to talk about template strings or t-strings with you today.
- 4:14Paul, welcome back.
- 4:15How many episodes have you been on? Many.
- 4:17I don't know. Not enough, because Brett's ahead of me.
- 4:21I know.
- 4:22Brett Cannon, you're not allowed to be on Talk Python for five years until I catch up, and then you can come back.
- 4:28Well, you should also send your message to Anthony Shaw, who I think is currently on the leaderboard, number one.
- 4:34He's been on a lot.
- 4:34Yeah,
- 4:35and then you were talking with Chris McDonough, who was like episode three or something
- 4:39like that way
- 4:40back in the day.
- 4:41Briefly, I'm Paul. Hi, I'm Paul.
- 4:44I am now head of developer advocacy at JetBrains, the PieCharm people.
- 4:48I make coffee for Jim and Dave on this project.
- 4:52I let Jim do all the big thinking, and I let Dave go out and explain to complainers that we heard the same complaints for f-strings back in the day.
- 5:02So this has been a super fun
- 5:04project to be on.
- 5:05Yeah, it's very exciting, and I'm super interested to dive into it.
- 5:10Something I didn't really know a lot about.
- 5:12Let's dive into it, right?
- 5:14what are t-strings and why do they exist?
- 5:20Weren't f-strings good enough?
- 5:21I mean, they look kind of like f-strings when we use them.
- 5:23Yeah, that's a great part about them, right?
- 5:25They look like f-strings.
- 5:27They behave a lot like f-strings in terms of technical issues like the scoping of the interpolation expressions, but they're not f-strings in a very profound way.
- 5:40One of the things that we wanted to prevent was stuff like, you know, the classic XKCD comic about the Bobby Tables sequel injection, okay, where it becomes a drop tables inadvertently.
- 5:54And we can get, yeah, exactly.
- 5:56The traditional exploits of a mom.
- 5:58This is your son's skull calling.
- 6:00Did he break something well in a way?
- 6:02Yeah.
- 6:03And so this is, you know, the original language of the PEP actually referenced directly Bobby Tables in this comic.
- 6:11For whatever reason, we removed that, but we don't have to do that today in this conversation.
- 6:17It was a big driver for this work.
- 6:20And the reason is that f-strings are great.
- 6:23They're great for formatting, but they're also sort of a trap, I think, for people to use because it's you're formatting things, everything is working.
- 6:33and, oh, by the way, you just introduced the HTML injection or SQL injection, or you just happen to have something that doesn't format properly because you didn't consider a particular corner case.
- 6:46What we really wanted to do was move a lot of that out of the usual user experience and into a library that could go, some library, something for HTML, something for SQL, something for some other domain-specific language that would allow it to go and handle that appropriately.
- 7:03We'll get into more about what that means, I guess, throughout this conversation.
- 7:08But that's really where we started.
- 7:09We wanted the simplicity of using f-strings.
- 7:14We can get into more about what that means, but without some of the hazards.
- 7:18Excellent. Excellent introduction.
- 7:20And it's important that this is part of the language.
- 7:23It's officially accepted in Python 3.14.
- 7:27Oh, will the jokes overflow about Py, this version of Python,
- 7:31when
- 7:31it comes
- 7:32out in the fall?
- 7:33It's a memorative edition of Python.
- 7:35We're so glad to get into it.
- 7:36That's right.
- 7:37It's super cool.
- 7:39So it's going to be part of the language.
- 7:41And the reason I bring this up is it seems like it needs to be part of the language, not just an external library, to really take advantage of it and not be just some API.
- 7:51You construct strings in some weird string builder way, but through literally just, quote, start typing stuff, that has to be part of the language, right?
- 8:00Yeah, that's exactly right.
- 8:01I mean, as Jim mentioned, f-strings are wonderful.
- 8:04The syntax is powerful, it's robust.
- 8:06It became very popular because of that.
- 8:09But of course, there are these foot guns of using them for, you know, SQL and HTML where you might run into some injection problems.
- 8:17With t-strings, the key difference is that while f-strings evaluate to the standard string type, t-strings evaluate to a new type that's shipping in Python 3.14 later this year called template.
- 8:29And so while they look like the literal form looks like any other string, they're quite different in that respect.
- 8:37And the key thing about the template type is it lets you, as a programmer, decide what parts of the string were statically authored and what parts of the string were dynamically substituted or interpolated.
- 8:49And so just being able to do that as a language feature is a really big deal.
- 8:53It's not something you can do with fstrings directly.
- 8:55And once you know those things, you can start to say, okay, this is a SQL statement.
- 9:00Maybe I need to do something with the interpolation like escape its value before I construct the final string that I'm going to send over to my database.
- 9:07Yeah.
- 9:08If somebody were to work with an F string, it does the cool interpolation at runtime.
- 9:14However, the thing that your API or any sort of code gets access to is just the resulting string that comes out.
- 9:23And I think this is probably the biggest piece of understanding that people need to wrap their heads around.
- 9:28And we'll dive into some of the things people do need to wrap their heads around and how it's different.
- 9:31But instead of getting just here's the string result, which who knows why it says --drop table there.
- 9:38Maybe that's what they want.
- 9:40You don't know.
- 9:41Right?
- 9:42But here you would have here's part of the string.
- 9:44And then here's a place where a value goes with a name.
- 9:47And that thing, if you look it up by name, its value is --drop tables.
- 9:53well, maybe we'll escape that or whatever.
- 9:55You know what I mean?
- 9:56Right.
- 9:57You and Brian covered it twice in Python Bytes.
- 10:01And the first time, I'm going to exaggerate a little bit, but Brian was like, what is this?
- 10:08Why
- 10:08do we need it?
- 10:09We have enough, right?
- 10:11The second time, Brian was like, oh, I get it now.
- 10:14There's going to be a lot of that, Paul.
- 10:15And we certainly
- 10:16will have some explaining
- 10:17to you.
- 10:17Yeah, there's going to be a lot of this.
- 10:18As Dave said, being able
- 10:20to get at this.
- 10:21Sorry.
- 10:21What's that?
- 10:21Sorry, there's going to be a lot of that.
- 10:23There's going to be a lot of like, we already have f-strings.
- 10:24What is this?
- 10:25Yep, sure.
- 10:26An F string gives you a string.
- 10:28A template string gives you a template.
- 10:30And then later you will get a string.
- 10:32Later means going through a function.
- 10:34They can go and do cool things for you like escape values.
- 10:38Or you're doing an HTML templating system.
- 10:41Pass it a dictionary and it will generate a style attribute with some policies along the way.
- 10:48pass it a false or a Boolean and it will elide the entire thing or do all kinds of other things.
- 10:54Having the template structure that Dave described gives business value DSLs a chance to step in and operate on structure rather than strings.
- 11:05So when I write this code, I say value equals T, some interpolated string sort of thing. Is it interoperable with a string in the sense that if I have a function that expects a string, could I pass it? Much like path from pathlib kind of goes where string file names go,
- 11:23or does
- 11:24the thing working with it need to know, no,
- 11:27it needs
- 11:27to be transformed, like you say, like process this to a SQL statement or process this to an HTML fragment, and then you get a string.
- 11:34Yeah, and for templates, the answer is, very intentionally, they are not the same as strings. And so library authors are going to have to update their SQL libraries and their HTML libraries to expect this new template type.
- 11:49And that's very intentional.
- 11:50The other thing I would say is that templates don't have a direct route to strings.
- 11:54So if you call str of template, you just get the default repper back, right?
- 12:00Which is probably not something you want to be showing your users or it's not HTML that you're trying to render.
- 12:06And that,
- 12:06again, is very intentional.
- 12:08It's also unlikely to cause injection attacks and whatnot as well.
- 12:12you're getting that
- 12:13reference. So much, you know, all those parentheses and braces and whatnot, not likely to happen.
- 12:19Yeah, absolutely. And so library authors will have to update their libraries to add new methods that expect this template type. And presumably those methods won't also take a string. You know, if you're concerned about injection vulnerabilities, presumably you want to take this new dynamic template type that tracks what parts of the string are interpolated and what parts came from the developer themselves.
- 12:41Right. And treat them differently. Yeah. Yeah. Very interesting. So let's just talk about that a little bit. I see some use cases in the chat, but maybe we could list out some more. So for example, Nym4 says structured logging, preventing cross-site scripting, SQL injection. What are some of the motivations here?
- 13:01Yeah, those are all great use cases. Any type of injection vulnerability. So SQL and HTML were obviously at the top of our minds.
- 13:09But another one that, in fact, there's potentially a new PEP coming around for that's being discussed right now on the Python discussion boards has to do with command injection.
- 13:20So when you shell out, you're worried about the user-provided values there as well.
- 13:26Give me a sec.
- 13:27I'll try and remember what the actual PEP
- 13:28number is.
- 13:29There's os.system,
- 13:31and then
- 13:31you can give it a command like echo some string from the user.
- 13:35But it could, of course, be some string, quote, ampersand, ampersand, rm-rf,
- 13:41or something like that, right?
- 13:44And of course, as Paul mentioned, once you have the tools to help you prevent injection vulnerabilities in sort of a more general way in this general purpose fashion, you also have the ability to do all kinds of other fun and powerful things like what Paul mentioned with HTML, which is, hey, I don't just want to write, you know, a single attribute.
- 14:02I want to give you a dictionary and you write 20 attributes based on the contents of my Python dictionary.
- 14:08And there's no reason you can't do that now, which is exciting.
- 14:11Yeah, I
- 14:11would add to that
- 14:12if you wanted to, for example, do some interesting complex analytical SQL, where you're using common table expressions, or even you just want to go and have your analytical SQL.
- 14:27I want to be able to do it on this column name versus some other column name.
- 14:30We want to make it possible to do that in a safe fashion.
- 14:34These are things that are difficult to express with some of the typical wrappers that we might use with SQL, like SQLAlchemy.
- 14:43Maybe like, I don't think you can really do like CTEs and Django query sets and whatnot.
- 14:48But what I'm trying to say is that both of those tools do allow you to access raw SQL.
- 14:53But now you need to be able to compose that raw SQL in a way that's safe.
- 14:57and uses the information about where this interpolation is happening in order to properly put it in place.
- 15:06So if you are in a situation where maybe that CTE is something you can access, you can go and say, okay, I can insert it into the SQL or maybe a little more common usage here for a lot of people.
- 15:20I don't know how many people use common table expressions.
- 15:22I do.
- 15:22is to go and, you know, I just want to, again, select from this particular table name or this particular column name in a safe fashion. Or I want to go and use values that can't be properly expressed with placeholders. But maybe if I use like a, you know, a temporary table to insert it in. These are all techniques that we use when we do this sort of work. But now we have this opportunity to bring it into directly in the library and then make it possible for users to use it without really having to think, where did this come from? How do I go and make this such that it can be used in a where in clause, for example, that sort of thing.
- 16:02This portion of Talk Python To Me is brought to you by the folks at Posit. Posit has made a huge investment in the Python community lately. Known originally for our studio, they've been building out a suite of tools and services for Team Python. Today, I want to focus on hosting your Python-based data science workloads. This includes dashboards, reports, plots, interactive web apps, all the way to custom Flask and Django apps. Their service is Posit Connect. Posit Connect makes it easy for data scientists to share work built with Python code. If you have a Streamlit app, Dash, dashboard, Plotly interactive plot, a FastAPI service, or even a Quarto report, just give Posit Connect the code it needs to maintain the asset and Connect automatically does the rest. Connect will manage your APIs and serve your interactive apps for you. And if you want, you can update your reports and dashboards on a scheduled basis. That's right. No need to explain to the stakeholders why that dashboard or plot stopped updating last week. You get to focus on your data science and leveraging your skill set while Connect makes you look good, keeping your code running and private.
- 17:11With Connect, you get a private URL on your Connect server, ensuring that your asset is continuously available to your shareholders. And you can control which users have access to the asset. Let Posit Connect handle the delivery and DevOps involved in sharing your work.
- 17:26You focus on what you do best. So if you work on a data science team, you owe it to you and your org to check out Posit Connect. Visit talkpython.fm/connect today and get a three-month free trial to see if it's a good fit. That's talkpython.fm/connect. The link is in your podcast player show notes. Thank you, Deposit, for supporting Talk Python To Me.
- 17:50If I may, real quick, I got to give Jim a lot of credit on this as we've gone through the years on this. And Jim taught programming languages at University of Colorado Boulder. And so he knows all these patterns and all these languages and the implementations from his Jython years and stuff. And even now, he's still talking in chat about, hey, you know, Last year he was talking about how domain-specific languages could apply to Pandas and other data science stuff.
- 18:18And now he's talking about LLM interactions and chat and stuff like that, where you've got a little language, but it's got a little bit of structure.
- 18:27And what if you could, and then 500, you know, Discord messages later, Jim has invented something new and interesting.
- 18:38So it seems to me like there's also some interesting possibilities for like lazy evaluation for also some possibilities for like query optimization.
- 18:50You know, Jim was talking about databases, but there's a lot of different things, both from a performance perspective, but also potentially just from a tightly versus loosely coupled systems.
- 19:01Like maybe you could say something to the effect of like, and the username goes here, but that part of the system logging doesn't know what the username is.
- 19:08But somehow as the template gets rendered, the additional pieces could get filled in with real details, like the IP and the username is actually this, because over here we know.
- 19:17But when the logging happened, we got a template that just said, well, whatever you figure out, it goes here.
- 19:23How much of those things are possible now?
- 19:24Like, could you look at the structure bit of SQL and say, well, if we reorder this, or if we put something about, you know, some kind of SQL hint or something about indexes or other things in there, could you maybe get more from it?
- 19:38I was thinking someone might else answer this, but I will go for this.
- 19:42I mean, I think that's possible to do.
- 19:45In terms, you know, often we, you know, again, if we're thinking about SQL, we would, you know, dispatch this to, you know, some sort of, you know, the internal optimizer and, you know, whatever database you're working with.
- 19:56But, you know, again, the, you know, again, using the SQL example, you know, there's, there are multiple ways that you could potentially interface some, you know, more interesting, complicated ways of interacting with SQL that are kind of hard to, you know, either you can't express easily with placeholders, you know, the standard name placeholders or positional placeholders in SQL, or just, you know, basically you find people going composing SQL.
- 20:26and, you know, doing it in a bad way.
- 20:28But if you were to go and, you know, there's like a popular tool called SQL Glock, for example, that allows you to take SQL, re-express it as an abstract syntax tree, and then what can you do with that?
- 20:42Well, you know, the interesting thing about, you know, what we do with t-strings is it can be a front-end to such tools.
- 20:50So t-strings themselves are, you know, intentionally designed to have just this very limited, but at the same time, you know, let's say, you know, very powerful, you know, integration with Python, right? You have, you know, effectively an f-string specification of, you know, what's going to describe that template. You describe the interpolations, they can be nested and everything, but fundamentally at the end of the day, you get this template object out of it, which could be nested and whatnot, but it's still this simple, straightforward thing. And then you pass it to a function that can do interesting things with that. From a performance perspective, that's also something that we intentionally put together. I don't know, Dave, if you want to talk about that, what we did specifically for that, because I think that might be something you want to speak to.
- 21:39Yeah, absolutely. Well, if you take a look at what's inside the template type, there are basically two tuples that sit there. There's a tuple called strings and a tuple called values or interpolations.
- 21:51You can kind of work both ways.
- 21:54And so as you might expect, the strings are the static parts that are part of the literal string that you specify.
- 22:00And the values or interpolations are the dynamic parts.
- 22:04And we kind of made it so that no matter how you write your literal, it's always the case that there is at least one string in that strings tuple, even if it's the empty string.
- 22:14So T quote, quote, gives you a template with no interpolations, with an empty tuple for interpolations, and with just a single quote for strings. And we ensure that there's always one more string than there are values or interpolations.
- 22:29And the reason we do that is that allows you to basically, the string's tuple becomes a useful cache key in many cases for memorizing. So if you process a template once with your HTML function, you might sit back and say, okay, I've seen this structure of HTML before, and the next time I do it, all I need to do is fill in the gaps. And so that we think is just like a really basic optimization that you can use. And we're seeing that use, Paul, you might want to mention the TDOM library that I know you and others have been working on in the last little while. But it's just a basic optimization that we can offer. I think it is worth calling out, though, that, and I know we mentioned this earlier, but probably worth calling out once more just to make it explicit, that t-strings like f strings are eagerly evaluated, right? So if you have hello,
- 23:22curly
- 23:23braces name, and name isn't in scope, you will get a name error just like with an f string.
- 23:29And so you might say, well, that feels a little weird to me because don't I want to reuse these things multiple times? And this is one of those cases where the word template is just an overloaded term in the computing world. And so if you're coming from the Jinja world or the Django templates world, you're probably used to the idea that I have this structure and I'm going to keep substituting values into it.
- 23:50And that's a little bit different than the other use of the word template, which comes from, say, the JavaScript side of the world.
- 23:56So JavaScript has tag template literals.
- 23:58If you've written any JavaScript code, you've probably run across them before.
- 24:03And there, like with t-strings, they're eagerly evaluated.
- 24:07So, you know, of course, you have the full generality of Python available to you.
- 24:10So you can just write a function that takes the parameters that you want to substitute and returns a T string, call it multiple times with different values, and you'll get different template instances out the other side.
- 24:20Probably just worth calling that out so that it's clear to listeners that there is this kind of funny overloading the word template.
- 24:27But yeah, back to the point about optimization.
- 24:30We're pretty excited about just the basic opportunities for building optimized template processing code, like an HTML function or a SQL function that works on top of this basic structure that we provide in PEP 750.
- 24:45Yeah, and I think one thing you can think about is, again, we may be getting a little too into the weeds here, but let's get into the weeds.
- 24:55You can think about the template as basically providing this interesting way of describing your arguments.
- 25:03And then you pass it to some sort of function, HTML, SQL, you name it.
- 25:09And basically, it's going to evaluate with respect to those arguments.
- 25:13But, you know, if you, so we basically, you know, if I have an HTML function, I'm going to want to look it up with that dot strings, template dot strings.
- 25:27Say I parsed this and I know how to go and work with this fragment.
- 25:31I already know where all the pieces need to go, the holes that need to be filled in by these interpolations, and it could be done very quickly.
- 25:41And so we've put a lot of effort in terms of thinking about what is that optimization path.
- 25:46And there's also related aspects.
- 25:48Why is this another reason this is built into the language is because you can get lexical scope as opposed to dynamic scope.
- 25:56You think that lexical, you know, you might think that dynamic scope is always the thing that's going to work for you.
- 26:01But no, this is the thing that brought Paul and I together on this project.
- 26:04It's exactly, you know, you can't use dynamic scope when you would expect it.
- 26:10So, yeah, if we want to go to that scope issue when calling components from list comprehension.
- 26:16So, Paul, if you want to speak to this a little bit, those are the problems you experienced.
- 26:21Sure. And this is, oh, my gosh, Jim in the first round of this and Dave in the second round of this in the PEP and the comments.
- 26:31This this damn thing had more comments on it than every other PEP in the history of civilization.
- 26:37Almost it felt that way.
- 26:39Major changes between the first draft and the second draft from lazy evaluation, tag template functions, all those things.
- 26:46But one that comes up pretty frequently is I could just do this with this script frame.
- 26:52And you can, but you can't and you shouldn't either way.
- 26:56But there is a nuance about scoping that bit me right here on the screen in this ticket that was the origin story of Gemini talking to each other.
- 27:062020, a predecessor that mimicked t-strings in sysget frame from the people behind the JavaScript package called HTM. They did a port to Python called HTMPy for doing powerful template languages kind of closer to the language, something that in the JavaScript world, this is a replacement for JSX. It doesn't need tooling. And so they wanted to bring that kind of experience, language-centric templating. They wanted to bring it to Python, but hit this issue about scope.
- 27:39Yeah. And it seems like it's just this obscure thing, but it isn't. You know, you use a nested function, it pops up. Use a generator expression, it pops up. Use a list comprehension, it pops up. So things that you might commonly want to use, now you need to know you shouldn't do that. So that's one thing. The second thing is it's slow. And it basically also breaks
- 27:59a lot of optimizations that have
- 28:01been introduced into Python in recent years.
- 28:04So you don't want to do that either.
- 28:06By using...
- 28:07And the
- 28:07creator of this hack, Barry said, don't use this hack.
- 28:10Well, I mean, there are
- 28:12good reasons to go and use sysgift frame, and especially there are historical good reasons.
- 28:17But I think we have, again, so it's something that comes up because, you know, if you have dug into the internals of Python, you've looked at the inspect, you know you can do certain things.
- 28:27And I think that's a fantastic way to explore new capabilities.
- 28:32But it is not necessarily what you want to go rely on because,
- 28:36again,
- 28:36the thing I would have taught in the class, what is the difference between dynamic and lexical scope?
- 28:42Well, here's where it really bites you.
- 28:46This portion of Talk Python To Me is brought to you by Auth0.
- 28:49Do you struggle with authentication?
- 28:52Sure, you can start with usernames and passwords.
- 28:54But what about single sign-on, social auth, integration with AI agents?
- 28:59It can quickly become a major time sink.
- 29:01And rarely is authentication your core business.
- 29:05It's just table stakes that you got to get right before you can move on to building your actual product.
- 29:10That's why you should consider Auth0.
- 29:12Auth0 is an easy to implement, adaptable authentication and authorization platform.
- 29:17Think easy user logins, social sign-on, multi-factor authentication, and robust role-based access control.
- 29:24With over 30 different SDKs and quick starts, Auth0 scales with your product at every stage.
- 29:31Auth0 lets you implement secure authentication and authorization for your preferred deployment environment.
- 29:37You can use all of your favorite tools and frameworks, whether it's Flask, Django, FastAPI, or something else, to manage user logins, roles, and permissions.
- 29:46Leave authentication to Auth0 so that you can start focusing on the features your users will love.
- 29:51Auth0's latest innovation, Auth4Gen AI, which is now available in developer preview.
- 29:56Secure your agentic apps and integrate with the Gen AI ecosystem using features like user authentication for AI agents, token vault, async authorization, and FGA for RAG.
- 30:08So if you're a Python developer or data scientist looking for an easy and powerful way to secure your applications, Get started now with up to 25,000 monthly active users for free at talkpython.fm/auth0.
- 30:22That's talkpython.fm/auth0.
- 30:24The link is in your podcast player's show notes.
- 30:26Thank you to Auth0 for supporting the show.
- 30:29Let's talk about HTML templates because I know, Paul, that was a lot of your interest in this, right?
- 30:34Indeed.
- 30:35Michael, you and I are like web originals,
- 30:38right?
- 30:39You're a big fan of Chameleon.
- 30:41Whenever you encounter a new web framework, the first thing you do is make a package for that that gives a
- 30:46chameleon support.
- 30:47That's right.
- 30:48You want FastAPI, just put the FastAPI chameleon decorator on there and you're back to have proper language.
- 30:55Yes.
- 30:56But my feeling has been that Python web development, particularly templating, is stuck in the early 2000s.
- 31:05I went and looked up recently.
- 31:06I think Jinja was first released in 2008.
- 31:10And there was a feeling back then that HTML and web design was something different than software.
- 31:17Separation of concerns, but more importantly, separation of people.
- 31:21You wanted to keep the templating people away from the software.
- 31:24You've got your designers and you've got your developers and they exchanged
- 31:27the CSS.
- 31:29I'm going to give you this sandbox where you can't break things.
- 31:32And it's going to be like Python, but not really Python.
- 31:36And the world doesn't work that way anymore.
- 31:38Front-end development is software.
- 31:41In the JavaScript world, it's software.
- 31:44It's components.
- 31:45It's tooling.
- 31:46It's testing.
- 31:48It's all of these things where people expect powerful tooling as part of software.
- 31:54We haven't.
- 31:54And there have been some things starting to get into this area, but this was what I was originally interested in, what Jim and I had talked about quite a bit.
- 32:02Dave comes from a similar background.
- 32:04And we want, in a general sense, we want DSLs, domain-specific languages, that are closer to Python.
- 32:12So when you say, here's a symbol, it's a symbol.
- 32:15And you know the scoping rules because it's just Python.
- 32:18It isn't some invented scoping rule.
- 32:20And you can use your formatter on it.
- 32:23And you can use mypy on it.
- 32:24And you can use all blah, blah, blah.
- 32:26You can get IDE assistance on it because it's just Python.
- 32:31And so that, from an HTML perspective and a web perspective, we're trying to move from PSP, JSP, PHP-style templating as a separate non-Python concern over into this new world.
- 32:46Dave, how did I do on that?
- 32:47Do you buy what
- 32:47I just said?
- 32:48I absolutely buy it.
- 32:49And like Paul mentioned, that's sort of how I came into this crazy rollercoaster ride, too, which is I had been working with a number of nonprofits over the last couple of years, building Python backends, doing a lot of front-end development with Python code.
- 33:02and feeling like, and in particular, one of the projects I worked on a couple years ago used HTMX.
- 33:08I think it was one of my first experiences with HTMX.
- 33:10And for those who haven't used it, you often write big templates and then occasionally you need to send lots of little blobs of HTML back.
- 33:18And it really starts to get difficult to use a tool like Jinja or a tool like Django templates to produce lots of little blobs of HTML while still keeping everything cleanly organized.
- 33:28You end up with lots of sort of include statements or Jinja macros.
- 33:33And those are great, powerful features, but they aren't quite as powerful as what Paul was saying, which is the full generality of the Python programming language to begin with.
- 33:41And so, you know, we started resorting to using HTML builders like HTTPY in my case, but there are a few others, Dominic and others.
- 33:51And those felt great, but they also felt to me like what building web stuff felt like in the JavaScript ecosystem circa 2010, right?
- 34:00Before tag template strings came along and ultimately in the JavaScript world before JSX came along as well.
- 34:07And so it did feel like there was really an opportunity to revisit that question.
- 34:10And that's sort of how I found Jim and Paul, who were thinking about this stuff and have been thinking about this stuff for far longer than I in the Python ecosystem.
- 34:19Yeah, very neat.
- 34:19I pulled up an example from Vue.js.
- 34:22And Dave, you mentioned HTMX.
- 34:25And one of the big ideas that Carson Gross from HTMX promotes is locality of behavior.
- 34:32Like I should be able to look kind of here on my editor and see all the stuff's going on.
- 34:37There shouldn't be four different places and then a convention that pulls in a fifth place that then makes a thing happen.
- 34:42And there's a lot of that in the JavaScript world of like, well, here's your behavior.
- 34:47And literally, here's the fragment of HTML that we're going to work on with capturing values and different substitutions and stuff.
- 34:55And it feels like new templating, and by templating, you're the one who said this silver letter, right?
- 35:00Yeah.
- 35:00New templating options like Chameleon 2 or Jinja 3 or whatever could come along and build on the ideas using some of the things here.
- 35:11Yeah, absolutely.
- 35:12And I'm really hoping we see an explosion of that as after Python 3.14 ships out the door.
- 35:18So a lot of this work that the three of you plus others, I want to be super clear that there are other folks at the top of this PEP here.
- 35:26Yes.
- 35:27About this who have also put a lot of work into it.
- 35:30Yeah, not just at the
- 35:31top of the pep, but also, of course, the broader Python community in the discussions without which.
- 35:37And then Paul mentioned the discussions are longer than for any PEP.
- 35:41But without that community feedback, I don't think we have landed
- 35:44where we are now.
- 35:45So if I print this, how long would this be?
- 35:49How many pages?
- 35:51206 pages to print the discussion of the TagStrings announcement.
- 35:56And that was the first.
- 35:57There were two discussions.
- 35:59Even better
- 35:59than Novella.
- 36:02Yeah, that's incredible.
- 36:03Also
- 36:04great
- 36:04feedback in other locations as well that we were also really trying to respond to.
- 36:10They were talking about Reddit or Hacker News.
- 36:14And a lot of credit to Jim on this.
- 36:17in the first round, Jim was really about lazy evaluation.
- 36:23And it was part of bigger ideas about inside out or outside in rendering and streaming and generators and a lot of things connected in his brain about it.
- 36:34And there was a lot of practical pushback, including Astral, about, hey, we're going to struggle to statically analyze that for Ruff.
- 36:41And Jim thought about it pretty hard and talked with Dave a lot and talked with others about
- 36:47rolled that back. Wow. So there's a difference between, there's a difference here between lazy, which is they're evaluated eagerly, these t-strings, but maybe, and async, right? So can we do async stuff on our strings? Like you have to call a function given a template to render text. That seems like you could just write that, yeah?
- 37:08Let me unmute myself. So yeah, I mean, the basic thing here is that these are expressions that can be evaluated, right? And so just because you have a, you know, I'll go for something that's even, you know, broader than this. You look at something like, you know, common pattern like Django's query set, right? So it's lazily evaluated until you actually go and actually look at its contents. A query doesn't even happen, right? In terms of against the underlying SQL engine that you're using.
- 37:43And in any event, the point is that we can have laziness where we Lambda wrap it.
- 37:49That was the original idea, and there's a lot of potential value in that.
- 37:54But there's also this aspect that, in general, the Python community has come up with a number of ways to incorporate laziness.
- 38:02And specifically through maybe it's an interval, Maybe it's a query set that is also inevitable.
- 38:09Maybe it's through the use of async expressions.
- 38:12These are all expressions that can be passed around and then used by, you know, something that is evaluating with respect to that.
- 38:21So t-strings don't change any of that, right?
- 38:24Because, you know, if you have that expression and it's been evaluated, but it's lazily, it's been lazily so, it can still be used for however you're going to work with that template in the function that operates on it.
- 38:39And I think this is just going to get at most of the things that I was thinking about when we were originally looking at this from this more lazily done way without the typing issues that ultimately we decided would make it too difficult to work with.
- 39:01So I think this is just something where we looked at what is the Python ecosystem?
- 39:07How have we solved these problems?
- 39:09and even though there was, you know, I really wanted to go and have something that basically would allow for maybe some greater functionality along, you know, doing like a deferred or lazy type capability implicitly, you know, that can be something else.
- 39:26We can bring, you know, there are discussions around like bringing in macros.
- 39:30It's, you know, you could implement that using coding on your file.
- 39:39I don't know where that goes.
- 39:40It might even interface with what we've done with t-strings.
- 39:43But the important thing is that you don't have to bring all that in in order to go and benefit from what we're doing with this.
- 39:52And
- 39:52by
- 39:52simplifying what we've done, I think we've, you know, come up with something that works a lot better. And I really valued the great feedback we had from the community as far as that was concerned.
- 40:02Yeah, thanks. One of the things I hear a lot mentioned throughout the conversations and the PEP and stuff is DSLs. So a domain specific language and SQL is kind of a domain specific language. But I think a lot of times people are thinking something I make that is really simple, right? Maybe I want to make a language to talk to simple robots, right? And I want to use this to construct maybe a way to talk to it that's way simpler, but easier to parse because you can say, well, here's where the where piece went or whatever, right?
- 40:33You can pull them out of the interpolations and the values and stuff.
- 40:36Can you guys talk to maybe some examples of what you had in mind or what might be possible there?
- 40:40This is good to me.
- 40:41So the Python has been a very popular tool for constructing two types of DSLs.
- 40:49One would be like the internal DSL that you see in Pydantic or SymPy, that sort of thing, where it's basically taking advantage of operator overloading.
- 41:00So let's put that aside.
- 41:02We'll look at the other version, which is this external DSL, where we think about, again, canonically HTML, SQL being pretty large targets.
- 41:12But if you look at the PyParsing project, you'll see, I don't know, how many dozens of languages it supports.
- 41:18Not to mention the fact that it's straightforward to write your own grammar in it and parse it into an abstract syntax tree.
- 41:30Yeah, I mean, it's just like, as it says here, the use of parsing expression grammars, you know, really makes it very straightforward to write a cursor.
- 41:42So I highly recommend anyone who's like interested in writing their own toy language or maybe have greater ambitions, look into tools like this, because I think this is one of the fantastic advantages we have in the Python community is we have really great libraries like this that are widely used.
- 41:59And so for your example of writing something that would interface with a robot, absolutely, absolutely.
- 42:06That's something that you could express.
- 42:09And you think about, would that potentially be even something where it could be useful for education, for example?
- 42:17And how would it work with, again, t-strings?
- 42:20Well, again, you would basically say there's this domain of things that we know from the Python space.
- 42:25We're computing maybe some velocity that we want the robot to move at.
- 42:31We're computing which color of brick that we want to pick up or whatever it happens to be.
- 42:40And we can basically say that that's something that's happening in our Python code directly.
- 42:46And then there's this language that we're using to talk to the robot.
- 42:50Okay. And what it looks like in terms of the t-strings is you basically are saying, okay, I'm going to go and parse this language fragment with some parser that can be made to use placeholders that will work with interpolations.
- 43:06And I'm going to parse it. I'm going to get an abstract syntax tree out of that. I'm going to use that to then evaluate how I'm going to go and say, you know, send in a number or color or other information to that toy language or more ambitious language.
- 43:23and then send out whatever is necessary in order to complete that communication with the system that's implementing that.
- 43:32So it's going to be great for people who really love languages and want to play with them.
- 43:38And again, I would start with something like PyParsing.
- 43:40That was a great example of the type of tool you could use here.
- 43:43Excellent.
- 43:44Yeah, I'll put that link in the show notes so people can check it out.
- 43:47Yeah, maybe bringing it all back home for a second or maybe just because I've got my head wrapped in building slide deck for a lightning introduction, lightning talk introduction to t-strings.
- 43:57But, you know, a common use case here when you're writing code that takes a template is not returning a string, but as Jim is kind of deeply hinting at here, parsing and returning some kind of abstract syntax tree.
- 44:09So in the HTML case, and you can actually find examples of this, for example, in the PEP 750 examples repository, there's an HTML function there and it has a bunch of nice features, but you're returning an HTML element back from the code that processes the T-string, you don't, in fact, have to grab a string out of a T-string right away or ever if you don't want to.
- 44:30So there's an immense amount of flexibility here that we're excited to see people use.
- 44:35Yeah. Dave, under your account on GitHub, Dave Peck.
- 44:39Needs
- 44:39to move.
- 44:40You know what? It's fine. It's just as good as anywhere.
- 44:44You have a bunch of examples of things you might do
- 44:47with
- 44:48these.
- 44:49and one of them is HTML.
- 44:51People can see how that might work and so on.
- 44:54And what are some of the other notable ones in here?
- 44:56I know I was looking through this earlier.
- 44:57Well, we wanted to show some basic examples like, hey, t-strings have a syntax that looks a lot like f-strings.
- 45:03How could you build f-strings on top of t-strings?
- 45:05So there is an F paren-paren method that's implemented here that just kind of helps you understand, okay, I'm writing dynamic code to do the thing that f-strings would have done at compile time anyway.
- 45:17We also have a structured logging example.
- 45:20And I'm excited about this, you know, structured logging that Python has shipped a logging cookbook for a long time since the Python 2.something era.
- 45:29And one of the examples in that logging cookbook is structured logging, by which we mean like, hey, I want to write out a sentence or two that people can understand, that humans can read.
- 45:39But I also have some values that I'm substituting in that sentence.
- 45:42And I'd really like that to be written out in some schema compliant way.
- 45:46And with t-strings, you can kind of do that in a really elegant way, which is to say you write the T-string once, you send it to some kind of method that expects t-strings on logging, and it emits basically the equivalent of an F-string.
- 46:01So that's the human readable bit.
- 46:03And then maybe it uses JSON or something to emit a structured output to a different log stream entirely.
- 46:09So one of the examples in the PEP and also in this PEP 750 repo is, you know, we send the human readable stuff to standard out and we send the machine readable stuff to standard error.
- 46:20And you can imagine, you know, your logging system is going to redirect those somewhere else over
- 46:24time.
- 46:24Yeah.
- 46:25I mean, the example here, you got standard error, but you could easily do a document database with JSON as well.
- 46:30And then put an index on action and say, show me all the actions
- 46:33taken
- 46:34that have this commonality in it.
- 46:36Right.
- 46:37and do a querying, oh, bam, rather than really error-prone, like substring matching of text or whatever, right?
- 46:43Yeah, and I'm really excited about it.
- 46:45You write the T-string once, and you get the benefit of both the human readable and the structured schema-compliant output, and that's really exciting to me.
- 46:54Yeah,
- 46:55and I guess if you use Postgres, you could just put it in a JSON field as well.
- 46:58Oh, yeah, we call that mullet
- 47:00schema, right?
- 47:01It's a business up front and partying back where the last column on every table is a JSON field.
- 47:07I've never heard that.
- 47:07That's incredible though.
- 47:09It's like, yeah, we look real formal, but don't look back there.
- 47:14Got it in the ponytail today.
- 47:15Yeah.
- 47:16Amazing.
- 47:18Okay.
- 47:19One thing I wanted to cover, there's a really interesting thing from somewhere in here.
- 47:24I have pulled this up, I believe.
- 47:26I'll just go to the PEP.
- 47:27And the PEP, there's a section that talks about how to teach this.
- 47:31And I think that that's, there's just a couple of ideas here that I think would make it really interesting for helping people understand better, right?
- 47:39Speaking to developers or maybe people who build libraries like web frameworks or logging libraries or something.
- 47:45Who wants to kind of give us a few of the ideas here that just maybe like, I know we touched on some of them a little bit.
- 47:52Yeah, I mean, I'm happy that I kind of stepped through the basics here, which is, you know, we touched on the fact that the syntax of t-strings and f-strings is essentially identical.
- 48:01Anytime you have an F, you can substitute with a T, but the result, the evaluated result is entirely different.
- 48:07You get a string or a template, and we've kind of talked about all the reasons why you might want to do that.
- 48:12I guess the key thing for developers to understand, and I'm going to divide developers into two groups here, those who use t-strings and pull in like an HTML or a SQL library, for example, and don't really think much about what's going on under the hood.
- 48:29For them, all they need to know is, hey, you write T of something and you send it to your SQL library and it's safe.
- 48:35And you don't really need to think much more about it than that.
- 48:38And then, of course, we had, sorry, but we had things like literal strings and other stuff in the type system that kind of tried to make type checkers check and
- 48:47catch when that would happen.
- 48:49Right. But this is more runtime catching because of the way it operates rather than, hey, it's a bad idea to do that.
- 48:56But, you know, Python typing can be ignored.
- 48:58So,
- 48:58yeah, it's probably worth mentioning the limits of that.
- 49:01I mean, template and interpolation, which are new types that ship with Python 3.14, are ones that you can cons up at runtime directly.
- 49:09You don't need to start with a literal.
- 49:11And so you can, of course, as a developer, do all kinds of goofy stuff and cons up all sorts of malicious templates if you want to.
- 49:18But generally speaking, if you're working from literals, which we sort of expect is the very common case, and you're using libraries that know about this new feature when Python 3.14 ships, generally speaking, you're staying on the safe side.
- 49:31You're staying in the bright, well-lit path.
- 49:33Yeah.
- 49:33One of the ideas I like to think about in APIs and design of software is like falling into the pit of success or falling into the pit of despair.
- 49:44You want to make it so the natural action is to fall into the pit of success and you've got to work your way out of there.
- 49:49You know what I mean?
- 49:50Yeah, you have to work hard to get there.
- 49:51Yeah,
- 49:51hopefully we've
- 49:52found a nice balance there.
- 49:53Yeah, it seems like it has.
- 49:55Yeah, I
- 49:55mean, I think from a teaching it perspective, you know, the pit of despair is so easy to fall into with f-strings, right?
- 50:02You know, so your structure logging, you know, I'm sorry, using f-strings in your logging is like, oh, I inadvertently log some passwords.
- 50:09Well, that's unfortunate, right?
- 50:10Or I, you know, I'm using f-strings with a SQL statement.
- 50:14It's like, oh, that really worked until it didn't.
- 50:17And so it's very easy to go down that into that pit of despair as you're describing.
- 50:21Whereas we're hopefully making it, you know, something where you have to work a little bit, know what you're doing, in fact, in order to go and actually do something that's bad.
- 50:31So, you know, I'm sure that, you know, someone can show me, you know, a couple steps that, you know, make it straightforward to get into that pit of despair.
- 50:42But I don't think it would be still the most obvious thing to do.
- 50:45So from a teaching perspective, we want to ensure that the most obvious thing to do is the right thing to do.
- 50:50And, you know, people can build libraries, you know, an HTML function or whatever in order to ensure that is, you know, going to work with that right path.
- 51:01But there's more.
- 51:01I think, Paul, you know, if you wanted to go and just how to teach this, I think part of this is just like, again, things like IDE integration and things like, you know, working with lenders and whatnot.
- 51:13That's also very exciting in terms of what can be done.
- 51:16And for me, that's kind of the exciting part.
- 51:17but there's a side about really lowering the bar for writing these functions that handle a template.
- 51:25You're not going to do anything with a template yourself.
- 51:28You're going to hand it to a function that's going to do something.
- 51:30And it's going to return a stringable, not a string, something that can be made later into a string.
- 51:36But it's the people on the other side that are going to be creating the values and the content and all that kind of stuff.
- 51:43And we have a real chance here to help them.
- 51:45We're not going to talk about double curly braces or single curly braces for the rest of time.
- 51:50It's single curly, just that alone.
- 51:53And what goes in there isn't going to be some invented language with a pipe that you send to safe or whatever.
- 51:59And you got to go memorize all these rules about how do I call a macro.
- 52:03It's just Python, which then means IDEs and tools and linters and formatters and type checkers all get to play a game.
- 52:12Go to mypy and file a ticket asking for type checking on Jinja.
- 52:17There is a ticket already and you can see their comments.
- 52:19They're not that interested.
- 52:21Same thing for a lot of other tools.
- 52:23But if they support 3.14, they support template strings and they support f-strings and they support this kind of everything because everything is just where it's supposed to be.
- 52:34And if I can go a little bit, Dave, is it okay if I go a little bit further on this riff about tooling?
- 52:39Let's see, you've got some points to make.
- 52:41Sorry, I'm on a roll.
- 52:43I'll try to wrap it up, I promise.
- 52:46This idea of a big ecosystem of tooling and all of that will give us a developer experience that's competitive again.
- 52:56All of these tools can start pitching in.
- 52:59I'm with the PyCharm team.
- 53:00Because this is f-strings with a T, they did it just like that.
- 53:05Dave did a fork of black where he got T string support basically just like that because it was a T instead of an F.
- 53:14Then we can get into some more interesting things like an HTML templating library.
- 53:18We are very fortunate in the world of Python to have Andrea over from JavaScript on the PyScript team.
- 53:25He's written 400,000 variations of JavaScript templating engines including tag template literals.
- 53:32He
- 53:32knows everything.
- 53:33He contributed a lot to this PEP such as that alternating thing.
- 53:37He's creating an HTML templating language that is browser first.
- 53:43Michael, you had a Vue.js up earlier that had like at click.
- 53:49There's going to be a lightning talk at PyCon about showing this in PyScript where something can run on the server and in the client and it's
- 53:58nuts.
- 53:58The whole thing is just...
- 54:00Server rendered templates, but also client-side stuff.
- 54:03Yeah, yeah,
- 54:04yeah, yeah.
- 54:05So there is a chance for us in the world of Python, it doesn't have to be our spelling, but if we can agree to structure and intermediate representations, Dave has ideas about how HTTPY could play ball.
- 54:19They can make components that we can consume.
- 54:21We can make components that they can consume.
- 54:23I'm really bullish on how we can change the developer experience in web development and more to be modern Python with tooling.
- 54:34Dave.
- 54:35I'm here for it. I'm here for it, Paul.
- 54:36Yeah, I'm just going to give you a what what here.
- 54:39Yeah.
- 54:41Yeah.
- 54:41I think if we really can unlock a language that works well on the front end, I know PyScript's come a long way, but I feel like we need a view-like thing, right?
- 54:52And
- 54:52it sounds like
- 54:52work is being done.
- 54:54I mean,
- 54:55if that comes along, there's going to be a lot of people that go, wait, we don't have to do JavaScript?
- 55:00Are you serious here?
- 55:01That would be pretty amazing.
- 55:02Just throw in your 100K MicroPython implementation of PyScript and
- 55:07go off
- 55:07to the races.
- 55:08Go ahead, Jim.
- 55:09Yeah, I was just going to say, and one other nice thing about t-strings and the fact that they look a lot like f-strings is that it's going to be a straightforward thing for us to, or I shouldn't say for us, but I think the community.
- 55:24I think there will be plenty of people who can work on this, fortunately, to incorporate t-strings into MicroPython.
- 55:33And now you, instead of having something that requires a somewhat heavier download in terms of CPython on the client side with PyScript, you can use MicroPython for that.
- 55:46I looked into it.
- 55:47It looks straightforward.
- 55:48I hope that we will see that development happen sooner than later.
- 55:52So a lot of opportunities to go and mix together client side and server side in some really interesting and very cool ways going forward with this work.
- 56:04What an interesting angle to say, we'll add t-strings to MicroPython.
- 56:09Yep, and keep that.
- 56:10So that
- 56:10it round trips to the front end of the browser in an efficient way.
- 56:14Yeah.
- 56:14Yeah.
- 56:15We've got a bunch of sprint days coming up with PyCon, so you never know.
- 56:21Okay.
- 56:21But Michael,
- 56:23when you mentioned that MicroPython is about 100K, which to my ears as an old school web person sounds huge, but also really tiny these days.
- 56:32Like when you build, when you do the default Next.js app install, which literally does nothing but ship a static looking page to you.
- 56:40Although, of course, static includes a whole bundle of React and other stuff with Next.js these days.
- 56:45That's about 130K.
- 56:47And that doesn't include an implementation of Python.
- 56:50Yeah.
- 56:50It's
- 56:51kind of amazing.
- 56:52Yeah, and I think it's pretty comparable to other things.
- 56:56I'm trying to poke around and find some examples.
- 56:59But yeah, I think 100K is certainly doable on a CDN and so on.
- 57:04It's a sweet spot.
- 57:05Yeah.
- 57:06Hey,
- 57:06Michael, I got one for you.
- 57:08And this is one that Jim and I started talking about and others piped in.
- 57:12Kudai, who's also working on this project, is doing stuff in this field.
- 57:17LLMs and code that needs to generate good LLM-generated results.
- 57:24If you had a template language that was closer to the language of Python and could be analyzed as such, and all the LLM and RAG tricks that people are applying to not just get code, but to get good quality code.
- 57:38Is this a moment where we could invent a web development story for Python that wasn't just cool because it was browser first?
- 57:48It was cool because it anticipates AI and generated code.
- 57:54You know, if you can infer types and things like that because you can look into the structure of the template.
- 58:01Yeah.
- 58:01and
- 58:02know things and they are Python rules inside the curly braces instead of invented stuff, you might have a better shot at machinery.
- 58:10Jim, what do you think?
- 58:11You had a lot to say on this.
- 58:12Yeah, I mean, I think a lot of people are looking at stuff around like in-context learning with how they work with prompts and everything.
- 58:22I do think that if I'm going to send a bunch of examples to like some sort of, you know, a mini shot, but I guess a few number of shots learning, it would be really nice to do it in a very structured way and really have good support for that.
- 58:39And so if you look at, you know, how people typically do this, well, they emit f-strings.
- 58:45And I'm sure it works most of the time.
- 58:46I just want it to work all the time.
- 58:49And so I think, you know, having something that can, you know, taint that structure, maybe even do some interesting, you know, like there's some stuff around like DSPY or whatever.
- 59:00I'm not certain actually how it's pronounced, but it basically does additional rounds of optimizing your in-context learning, prompting in general.
- 59:12Having access to that structure might be very helpful for it.
- 59:15Who knows?
- 59:16I think that there's a lot of interesting opportunities in this space where we maintain this structure, we're able to reflect on it, And then use that for whatever sort of interaction we have with other systems.
- 59:30And fundamentally, that's what this supports.
- 59:33Because you have access to those Python structures.
- 59:36You can figure out what that's supposed to be before you go and send it off to somewhere else.
- 59:40So what does that mean in terms of capabilities?
- 59:43It's very interesting.
- 59:44Paul, when you were talking about typing earlier and then talking about mypy, it just made me realize or think, I guess I thought about it before, But reemphasize that for me, the typing is a communication tool to me, mostly through things like PyCharm, but not 100%, but mostly through the editor saying, here's what it's supposed to be here.
- 1:00:05Here's what this is supposed to do.
- 1:00:06So I don't have to go to definition and try to decipher it.
- 1:00:10And I almost never use mypy or one of these Pyright or Pyre or, you know, you name it, because that's not my goal.
- 1:00:16My goal is to add typing so that I can stay out of the docs, except when I really must.
- 1:00:21And the same applies to what you share with chat and agenda code generators.
- 1:00:26They can look at those types and they can see much more information and it can make them way more accurate and productive, right?
- 1:00:33And I feel like this could communicate more information to them, like you were saying, so they have more structure to work with rather than just, here's a string, I don't know what it is.
- 1:00:43Can I riff on that for a second?
- 1:00:44Yes, riff.
- 1:00:45This is all not news to Dave, who he and I are writing our lightning talks and FlaskCon talk together.
- 1:00:54You're around on Friday afternoon.
- 1:00:56Come see Dave.
- 1:00:57Come see me.
- 1:00:57It's going to be mind
- 1:00:58-blowing.
- 1:00:59Lightning talks are going to be mind-blowing, too.
- 1:01:01Don't they normally publish those videos from FlaskCon as well?
- 1:01:04I think they do.
- 1:01:05Maybe.
- 1:01:06He needs help.
- 1:01:06Okay.
- 1:01:07Well, I'm hoping.
- 1:01:08Poor David is doing 500 jobs at once.
- 1:01:11Everyone support David Lord.
- 1:01:13I'm going to be giving a demo in PyCharm, at least, of T-String support with interpolation autocomplete and navigation and all the PyCharm tricks, but also a experimental plugin that does component support with TDOM.
- 1:01:30So it looks just like JSX, where you pass props to something and it obeys the can do not just autocomplete of the props, But if you're missing a required prop, it will know that because it looks at the function signature of the component.
- 1:01:46And it will match the types of the prop.
- 1:01:48If you pass a string for an int, it will squiggle you.
- 1:01:51In the future, we can go further like a lot of IDs can do and extract components to be standalone, to get big components into small components, all kinds of things.
- 1:02:02Once you get into the world of tooling, you know, you've got typing, you've got signatures, you've got all of these great things.
- 1:02:09mouse over for doc strings and doc hints and help.
- 1:02:14Lots of help.
- 1:02:14That's awesome.
- 1:02:15Well, I'm looking forward to it.
- 1:02:16Guys, we're about out of time.
- 1:02:18I want to wrap this up with one final thought here.
- 1:02:21So speaking to several audiences, let's say two audiences at the moment, give us some advice for potential adopters.
- 1:02:28So if I'm Sebastian from FastAPI or David from FlaskCourt or I'm some other library adopter, SQL, MicroSQLAlchemy, right?
- 1:02:38Or that's audience one.
- 1:02:40Audience two would just be people who thought f-strings were awesome and this is even more awesome.
- 1:02:45They want to try it in their code.
- 1:02:46What are some pitfalls and best practices for these folks?
- 1:02:49And what are the opportunities there?
- 1:02:51Well, I mean, I think, you know, in general, the fact that t-strings exist doesn't mean that f-strings need to disappear tomorrow.
- 1:02:58So I guess that would be the first thought I'd have for the community that isn't actively building libraries that might benefit from T string functionality, which is that the
- 1:03:06tool is
- 1:03:06still useful.
- 1:03:07And in fact, there are many cases where probably what you really want is an f-string rather than a t-string.
- 1:03:12It's exactly
- 1:03:13these cases where you want the interpolations to do something special, whether that's prevent injunction vulnerabilities or, you know, transliterate, you know, a single dictionary into a bunch of attributes in HTML where you might want to start playing with t-strings.
- 1:03:29And there, you know, I think if you're a library developer and you happen to be at PyCon, come say hello.
- 1:03:35we'd love to talk to you and kind of figure out how you're thinking about adopting these.
- 1:03:39I think the biggest one that came up in discussion amongst library developers that I saw was just, wait, how do I type my function?
- 1:03:46So if I'm writing a SQL layer and I've got an execute method, right now that takes a string or maybe it takes, you know, Michael, you mentioned a little string.
- 1:03:53It takes some variety of some special type that my library supports.
- 1:03:58Should I also take a template there?
- 1:04:00Should I allow a union of both?
- 1:04:02Probably that's a foot gun.
- 1:04:03you probably really want to think about separating these things out.
- 1:04:06And so I think we'll see best practices like that kind of emerge over time.
- 1:04:10Yeah, I would add a couple things.
- 1:04:12One, we actually love f-strings.
- 1:04:16When you
- 1:04:16look at the actual code that implements the t-string functionality in terms of like a library, like an HTML function or whatever, what do you find in there?
- 1:04:26Lots of uses of f-strings to actually go and pump out the actual string that you need.
- 1:04:33It's just done in this, you know, disciplined fashion that we can do because we have carefully separated out when the interpolations are being actually evaluated in that context, right?
- 1:04:45Second thing to think about is in terms of like a good best practice would be, you know, if you have a T-string, you know, and you say HTML, well, I don't think HTML should just like do something there or SQL or whatever.
- 1:05:01It should go and allow it to prepare, you know, a document object model or, you know, prepare a SQL query or whatever.
- 1:05:10And then you execute it or render it or whatever.
- 1:05:14So I would differentiate this sort of setup versus, you know, some final rendering would be a typical best practice that certainly I would recommend.
- 1:05:24And there are obviously more, you know, another best practice definitely is at some point, you probably want to think about the optimization of these things, right?
- 1:05:33And so we really put in some thought into how the template strings can be used as a memorization key efficiently.
- 1:05:43So you can look it up in terms of an LRU cache.
- 1:05:46Now, this is no different than if you looked at the internals of, say, the RE module, you know, sports regular
- 1:05:52expressions.
- 1:05:53Yeah.
- 1:05:54You know, there is this, at one point, you actually had to go into RE compile.
- 1:06:00And it's not a bad practice to do it, but if you look internally at it, it actually does the RE compile for you.
- 1:06:06So you don't pay the overhead of running through this Python parse every single time.
- 1:06:13Interesting. I didn't know that.
- 1:06:15Yeah.
- 1:06:15Yeah.
- 1:06:16But the actual implementation of that in terms of running the regular expression is done through this bytecode virtual machine that's specific to regular expressions.
- 1:06:30and can be very efficient.
- 1:06:31So again, we'll see some of these sorts of separations where we'll think about what it means to go and have a fast path where you actually are evaluating this HTML fragment or whatever, or working with SQL.
- 1:06:45And then there's some sort of, well, you have to pay the parse penalty at some point.
- 1:06:49But part of paying that parsing penalty is ensuring that things are done in a safe fashion, being written out in a structured way.
- 1:06:58So that's, you know, more than makes up for it compared to, again, just, you know, just using f-strings and hoping that your SQL statement didn't include, you know, in that interpolation, the Bobby tables evil injection.
- 1:07:13Yeah, excellent.
- 1:07:14For me, I'd say first a shout out to our PEP sponsor and the hero for the second half of all this, Lissandros, has been marvelous.
- 1:07:25getting the implementation and shaping in our interface to the rest of the core team and all that kind of stuff.
- 1:07:30Really fast turnaround, really joy to work with.
- 1:07:34Dave and Lissandro spent a lot of time ironing out all the little things.
- 1:07:38I think about this whole path since Guido and Jim sat down at a PyCon like three years ago on the implementation.
- 1:07:44I think it's been, this will be like the fourth PyCon since this whole thing first started.
- 1:07:50And Jim and Guido kind of laid the groundwork for where we are right now, which is t-strings is just the start.
- 1:07:58t-strings by itself ain't that much.
- 1:08:01And I think, Jim, you made some decisions to intentionally defer to future PEPs some of the things.
- 1:08:08Jim just said about these functions that don't immediately render.
- 1:08:14They prepare an intermediate representation that might later render.
- 1:08:18For me, that's my interest in this PyCon.
- 1:08:21Let's build a community.
- 1:08:22Let's build a community around interoperability so HTPy doesn't have to spell things our way, but we can talk to each other because we agree on a node interface or something like that for a DOM representation.
- 1:08:34The front-end world has gotten great value out of pluggability.
- 1:08:38The Vite system, for example, is a processing chain with a documented intermediate representation.
- 1:08:45It's easy to write a plug-in to go grab that and hack on it.
- 1:08:48You want to turn everything into a relative URL?
- 1:08:51Write a little plug-in, put it in the chain, and you'll be handed something that lets you operate on things.
- 1:08:57We can do something great together.
- 1:08:59We don't all have to do the XKCD to invent a new standard to replace all the other standards.
- 1:09:06We can still work with Jinja.
- 1:09:08We could still work with HTTPY and some of these other things, and hopefully we'll see some of that at PyCon.
- 1:09:14Awesome. Thank you, guys.
- 1:09:16And we're specifically recording this on a time frame that hopefully...
- 1:09:21Thank you so much, Michael.
- 1:09:22If people are dutiful and on top of their listening, we'll listen to this right before they go to PyCon.
- 1:09:28So I would like to encourage them to reach out to all of you and maybe go to some of the lightning talks.
- 1:09:34And if they have ideas, participate.
- 1:09:35Because now is the time.
- 1:09:36Thanks, Michael, for doing this.
- 1:09:38And we're excited to meet everybody at PyCon who's heading out that way.
- 1:09:40Yeah.
- 1:09:41Paul, you're welcome.
- 1:09:41And you too, Dave.
- 1:09:43Jim, Dave, and Paul, thank you all for being here.
- 1:09:46See you later.
- 1:09:47All
- 1:09:47right.
- 1:09:47Thanks for having us.
- 1:09:48Thank you.
- 1:09:49Bye-bye.
- 1:09:50This has been another episode of Talk Python To Me.
- 1:09:54Thank you to our sponsors.
- 1:09:55Be sure to check out what they're offering.
- 1:09:56It really helps support the show.
- 1:09:59This episode is sponsored by Posit Connect from the makers of Shiny.
- 1:10:03Publish, share, and deploy all of your data projects that you're creating using Python.
- 1:10:07Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, reports, dashboards, and APIs.
- 1:10:14Posit Connect supports all of them.
- 1:10:16Try Posit Connect for free by going to talkpython.fm/posit, B-O-S-I-T.
- 1:10:23And it's brought to you by Auth0.
- 1:10:25Auth0 is an easy-to-implement, adaptable authentication and authorization platform.
- 1:10:30Think easy user login, social sign-on, multi-factor authentication, and robust role-based access control.
- 1:10:37With over 30 SDKs and quick starts, Auth0 scales with your product at every stage.
- 1:10:43Get 25,000 monthly active users for free at talkpython.fm/auth0.
- 1:10:49Want to level up your Python?
- 1:10:51We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:10:55Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:11:00And best of all, there's not a subscription in sight.
- 1:11:03Check it out for yourself at training.talkpython.fm.
- 1:11:05Be sure to subscribe to the show, open your favorite podcast app, and search for Python.
- 1:11:10We should be right at the top.
- 1:11:12You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:11:21We're live streaming most of our recordings these days.
- 1:11:24If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:11:32This is your host, Michael Kennedy.
- 1:11:34Thanks so much for listening.
- 1:11:35I really appreciate it.
- 1:11:36Now get out there and write some Python code.