Appetite for a backport of t-strings/PEP750 for python<3.14?

I am wondering if

  • there is already work underway somewhere for a backport of strings.Template for python versions older than 3.14
  • if there isn’t, if anyone has advice on how to make that happen.

I am working on ibis, which is a dataframe API that generates and then executes SQL on various underlying SQL engines. In this recent issue, I think I came across a good use case for strings.Template. But, I want to make this available to users who aren’t using 3.14+.

So, I was wondering if there was a backport for these older python versions, and then we could make ibis accept either implementation (probably using a Protocol for the type hints).

If this doesn’t exist, I’m wondering if there’s appetite from the rest of the community, and suggestions on how to implement this. Maybe take GitHub - asottile-archive/future-fstrings: A backport of fstrings to python<3.6 as inspiration? Other potential gotchas or things to watch out for if I make something?

Thanks all!

4 Likes

I’m pretty sure you’re not referring to the string.Template class, which has been around for a very long time and needs no backporting. Are you asking about string.templatelib which is new in Python 3.14?

3 Likes

Template strings are a new feature in Python 3.14 (not yet released). As such, it won’t be backported by us to a previous version. It might be possible to use tricks à la future-fstrings to do so, but this would be a third-party endeavour. I’m not aware of anyone else who has asked about doing this yet.

A

I have created:

https://pypi.org/project/tstrings-backport/

While creating the package in PyPI, I found another one, which may be more advanced than mine:

https://pypi.org/project/future-tstrings/

3 Likes

Thanks, GitHub - abilian/tstrings-backport: Backport of t-strings (PEP 750) is exactly what I was wondering if it exists. @sfermigier I will continue in the issues of that repository for now to see if it will fit my needs.

I am also interested in potential backports since for production use we would not default to Python 3.14 for at least another 6 months to a year. It’ll be cool to see how Ibis make use of it for SQL templating :slight_smile:

For reference I found the github source for future-tstrings: GitHub - mkzeender/future-tstrings: A backport of tstrings to python <3.14. Notably can parse literal syntax t”…” but seems this magic has limitations in non-standard environments like aws lambda or needs pre-compiled.

@NickCrews Perhaps the title of this post could be edited for clarity, to mention one or all of t-strings, Template strings and PEP750 rather than string.Template. It is defintely unfortunate the naming is confusingly similar…

Regarding the naming of this thread, yes it appears I chose a bad name, but I think I’m not allowed to change it now :frowning:.

@sfermigier and I have done a few more tweaks to abilian/tstrings-backport, so now (please file an issue if not) that package is a drop in replacement for the stdlib implementation. That’s what I am going to use whenever I need a backport. I chose that over mkzeender/future-tstrings because I didn’t like the magicalness of transpiling the syntax at import time.

Here is the PR for how I’m trying to bring this into ibis if you are curious :slight_smile:. Note that for these tests I just vendored in the entire tstrings implementation from that backport in order to avoid a new dependency for ibis. So if you are doing something similar, that seemed to work pretty well since it’s just a single file.

4 Likes

I’ve renamed the thread (not sure why I have permission to do that but apparently I do). Let me know if it’s not what you wanted.

You have the ‘Regular’ trust level, which allows one to rename or move topics.

A

I’ve filed a couple of issues. There’s at least one gap that I spotted first time I looked.


Semi-relatedly:

Hopefully we should also get t-strings into the next Cython release. Because that goes through our own parser it’ll work on any Python version that we support. Although obviously compiling to a binary extension module is a fairly heavy step just to get backported t-strings so I’m not promoting it as the first choice.

As part of that we’ll monkey-patch our own implementation of Template and Interpolation into string.templates. One thing I’m keen to ensure is that we just use the generic interface. So if someone has monkey-patched an alternate implementation first then we’ll respect that and just use their classes.

Hi, I am the author of mkzeender/future-tstrings Feel free to ask me anything, or suggest improvements to the library. You can also open github issues if you want. I will release a stable version shortly after the python 314 release date.

The limitations are that in non-standard environments it won’t install itself automatically. Other than that it should be fine.

we already have the _future_ module. we could do from __future__ import tstr

But its better to wait to see how it performs in 3.14 before even considering backwards distribution