Skip to content

Commit

Permalink
Introduce templatelib, a new top-level standard library module.
Browse files Browse the repository at this point in the history
This is after discussion amongst PEP authors.
  • Loading branch information
davepeck committed Oct 21, 2024
1 parent 67eca55 commit c7af6ab
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions peps/pep-0750.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This PEP introduces template strings for custom string processing.

Template strings are a generalization of f-strings, using a ``t`` in place of
the ``f`` prefix. Instead of evaluating to ``str``, t-strings evaluate to a new
type, ``types.Template``:
type, ``Template``:

.. code-block:: python
Expand Down Expand Up @@ -107,13 +107,14 @@ Template String Literals
------------------------

This PEP introduces a new string prefix, ``t``, to define template string literals.
These literals resolve to a new type, ``types.Template``.
These literals resolve to a new type, ``Template``, found in a new top-level
standard library module, ``templatelib``.

The following code creates a ``Template`` instance:

.. code-block:: python
from types import Template
from templatelib import Template
template = t"This is a template string."
assert isinstance(template, Template)
Expand All @@ -131,8 +132,7 @@ see the `Raw Template Strings`_ section below for more information.
The ``Template`` Type
---------------------

Template strings evaluate to an instance of a new type, ``Template``, found
in the ``<TBC>`` module (proposed: ``types``):
Template strings evaluate to an instance of a new type, ``templatelib.Template``:

.. code-block:: python
Expand Down Expand Up @@ -165,7 +165,7 @@ The ``Interpolation`` Type
--------------------------

The ``Interpolation`` type represents an expression inside a template string.
Like ``Template``, it is a new concrete type found in the ``types`` module:
Like ``Template``, it is a new concrete type found in the ``templatelib`` module:

.. code-block:: python
Expand Down Expand Up @@ -264,7 +264,7 @@ interpolations in uppercase:

.. code-block:: python
from types import Template, Interpolation
from templatelib import Template, Interpolation
def lower_upper(template: Template) -> str:
"""Render static parts lowercased and interpolations uppercased."""
Expand Down Expand Up @@ -528,7 +528,7 @@ specifiers like ``:.2f``. The full code is fairly simple:

.. code-block:: python
from types import Template, Interpolation
from templatelib import Template, Interpolation
def convert(value: object, conv: Literal["a", "r", "s"] | None) -> object:
if conv == "a":
Expand Down Expand Up @@ -603,8 +603,8 @@ We can implement an improved version of ``StructuredMessage`` using template str
.. code-block:: python
import json
from types import Interpolation, Template
from typing import Any, Mapping
from templatelib import Interpolation, Template
from typing import Mapping
class TemplateMessage:
def __init__(self, template: Template) -> None:
Expand Down Expand Up @@ -660,7 +660,7 @@ and a ``ValuesFormatter`` for JSON output:
import json
from logging import Formatter, LogRecord
from types import Interpolation, Template
from templatelib import Interpolation, Template
from typing import Any, Mapping
Expand Down Expand Up @@ -819,7 +819,7 @@ best practice for many template function implementations:

.. code-block:: python
from types import Template, Interpolation
from templatelib import Template, Interpolation
def process(template: Template) -> Any:
for arg in template.args:
Expand Down Expand Up @@ -1133,6 +1133,15 @@ We rejected this in favor of the simpler approach of using plain ``str`` and
allowing combination of ``r`` and ``t`` prefixes.


Other Homes for ``Template`` and ``Interpolation``
--------------------------------------------------

Previous versions of this PEP proposed that the ``Template`` and ``Interpolation``
types be placed in the ``types`` module. This was rejected in favor of creating
a new top-level standard library module, ``templatelib``. This was done to avoid
polluting the ``types`` module with seemingly unrelated types.


Enable Full Reconstruction of Original Template Literal
-------------------------------------------------------

Expand Down

0 comments on commit c7af6ab

Please sign in to comment.