You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+6-152Lines changed: 6 additions & 152 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,36 +4,11 @@ This repository contains full implementations of the code examples described in
4
4
5
5
## Running This Code
6
6
7
-
This repo has a [devcontainer](https://containers.dev) definition that makes it easy to run the examples. The devcontainer includes a [fork of cpython 3.14a7+](https://github.com/lysnikolaou/cpython/tree/tstrings) that provides a prototype implementation of PEP 750.
7
+
You'll need [Python 3.14](https://www.python.org/downloads/release/python-3140/), which was released in October 2025, and includes support for t-strings.
8
8
9
-
It's easy to run this code yourself:
9
+
If you have [Astral's `uv`](https://docs.astral.sh/uv/) installed, you can run 3.14 in an isolated environment with `uv run --python 3.14 python`.
10
10
11
-
1. Make sure you have Docker installed
12
-
2. Clone this repository and open it with `vscode`
13
-
3. When asked, say that you want to re-open _inside_ the devcontainer
14
-
15
-
After the container is initialized, make sure that everything works by opening up a terminal in vscode. This will open in the running docker instance. Then:
16
-
17
-
```
18
-
/workspaces/pep750-examples# python --version
19
-
Python 3.14.0a7+
20
-
/workspaces/pep750-examples# pytest
21
-
... (hopefully, all tests pass!) ...
22
-
```
23
-
24
-
Congrats; you're good to go!
25
-
26
-
## A Word About the Code
27
-
28
-
This repository builds on top of the @lysnikolaou's [cpython branch implementing t-strings](https://github.com/lysnikolaou/cpython/tree/tstrings).
29
-
30
-
### Linting, formatting, etc.
31
-
32
-
The included devcontainer loads my [fork of black with t-string support](https://github.com/davepeck/black/tree/pep750-support).
33
-
34
-
`isort` is used.
35
-
36
-
Tools like `mypy` and friends can't type check t-strings yet, hence the many extra `: Template` annotations sprinkled throughout the code.
11
+
You can run tests on this repository `uv run pytest`.
This [example is described in detail](https://peps.python.org/pep-0750/#example-implementing-f-strings-with-t-strings) in PEP 750.
@@ -85,7 +61,6 @@ See the tests in [`test_logging.py`](./pep/test_logging.py).
85
61
86
62
This [example is described in detail](https://peps.python.org/pep-0750/#example-structured-logging) in PEP 750.
87
63
88
-
89
64
### Working with old-style format strings
90
65
91
66
The code in [`format.py`](./pep/format.py) shows how to convert old-style format
@@ -101,133 +76,12 @@ assert f(as_template) == "Thank you Alice for spending $42.00."
101
76
102
77
The `from_format()` function supports essentially all the features of old-style format strings, including positional and keyword arguments, automatic and manual field numbering, index and dot interpolation notation, nested format specifiers, and more.
103
78
104
-
105
79
### HTML Templating
106
80
107
81
There are several short "HTML templating" examples in [PEP 750](https://peps.python.org/pep-0750/).
108
82
109
83
They all use a hypothetical `html()` function that parses template strings to an intermediate type, `Element`, and supports context-dependent processing of interpolations.
110
84
111
-
A real working implementation of `html()` is found in this repository's [`web.py`](./pep/web.py). Corresponding tests are found in [`test_web.py`](./pep/test_web.py).
112
-
113
-
Building a full robust HTML templating package on top of template strings is both a noble goal _and_ beyond the scope of this example code. Instead, our goal is to hint at some interesting uses of template strings in the HTML context, and to provide an early roadmap (warts and all) for how a more robust package might be built.
114
-
115
-
The `Element` class is a simple representation of an HTML element:
116
-
117
-
```python
118
-
119
-
from dataclasses import dataclass
120
-
121
-
@dataclass(frozen=True)
122
-
classElement:
123
-
"""A simple representation of an HTML element."""
124
-
125
-
tag: str# An empty string indicates a fragment
126
-
attributes: Mapping[str, str|None]
127
-
children: Sequence[str| Element]
128
-
129
-
def__str__(self) -> str:
130
-
...
131
-
```
132
-
133
-
Elements can be converted to strings:
134
-
135
-
```python
136
-
element = Element(tag="p", attributes={}, children=["hello"])
The `html()` function supports several features that decide how interpolations are processed based both on their type _and_ their position in the HTML syntax.
153
-
154
-
Content can be interpolated into the body of an element:
155
-
156
-
```python
157
-
text ="Hello, World!"
158
-
element = html(t"<p>{text}</p>")
159
-
assertstr(element) =="<p>Hello, World!</p>"
160
-
```
161
-
162
-
When content is interpolated, it is also automatically escaped:
As a convenience, `html()` also supports a simplification for nesting: if an interpolation's value is a `Template`, it is automatically converted to an `Element`:
Tag name interpolation allows us to support a simple form of "components". For instance, we can define a `Magic()` function that alters both the attributes and children of an element:
This repository _used_ to contain a real working implementation of `html()`, but it was for demonstration purposes only. We have since built a much more [robust HTML templating package, `tdom`](https://github.com/t-strings/tdom), which is based on the same ideas but is much more fully featured. We hope that, in addition to being a useful package in its own right, `tdom` will also serve as a more complete reference implementation of the ideas described in PEP 750.
232
86
233
-
The `html()` template processing code sees that `{Magic}` is an interpolation, that it occurs in the tag position, and that its value is a `Callable`. As a result, `html()` calls `Magic()` with the interpolated children and attributes and uses the result returned _by_`Magic()` as the final `Element`.
87
+
(You can find the older HTML templating examples in the commit history, in case you still want to see them.)
0 commit comments