by masklinn · 2y ago · 0 comments
Compared to what?
At the end of the day you're still doing string formatting, if you want parsing, then you'd feed the item into a parser, which this doesn't preclude.
The interface sounds a lot better than JS's anyway, as that completely separates the literal strings and the interpolations so you have to re-intersperse them which is muggy.
> interpolations become four-tuple-like
They become an Interpolation object, which can be unpacked if you find that more convenient, but you can access the members if you prefer:
- 0 is getvalue is the callable to retrieve the evaluated expression
- 1 is expr is the raw text form of the expression
- 2 is conv is the !conversion tag (s, r, or a)
- 3 is format_spec
1. Parse to an AST, generally using an off-the-shelf parser. In practice, it's possible to rewrite interpolations with a placeholder suitable for a given language, eg x$Nx for HTML. Of course if that doesn't actually work, you might have to write/modify an existing parser. Hopefully we can cleverly avoid this extra work.
2. Walk/compile the AST, filling interpolations, but taking in account the context. This can for example take the form of building appropriate query strings that avoid Bobby Tables SQL injection, whether by mapping to SQL placeholders or with appropriate quoting (such as for a column or table name).
3. Memoize these steps, much as we see with builtin DSLs in Python, like the re module; see https://github.com/python/cpython/blob/3.12/Lib/re/__init__.... We do plan to make this easier/faster by supporting getting the original source of the template string (Template.source), vs the *args approach we show in the PEP at the start of this discussion (this will become Template.args instead; Template here is the proposed protocol of the object passed in).
Related is my post here: https://discuss.python.org/t/pep-750-tag-strings-for-writing...