Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor update to the PEP #17

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions peps/pep-0750.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The ``Template`` Type
---------------------

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

.. code-block:: python

Expand All @@ -137,7 +137,7 @@ any interpolations in the literal:

The use of ``@dataclass`` in the definition of ``Template`` above (and
``Interpolation`` below) is meant to be suggestive; the exact implementation
in ``cpython`` may differ, but developers can expect that ``Template`` instances
in CPython may differ, but developers can expect that ``Template`` instances
can be constructed and utilized in the same way as a typical dataclass.

See `Interleaving of Template.args`_ below for more information on how the
Expand All @@ -154,7 +154,7 @@ Like ``Template``, it is a new concrete type found in the ``types`` module:

@dataclass(frozen=True)
class Interpolation:
value: Any
value: object
expr: str
conv: Literal["a", "r", "s"] | None = None
format_spec: str = ""
Expand Down Expand Up @@ -202,6 +202,17 @@ As with f-strings, this is an arbitrary string that defines how to present the v
template = t"Value: {value:.2f}"
assert template.args[1].format_spec == ".2f"

Format specifications in f-strings can themselves contain interpolations. This
is permitted in template strings as well; ``format_spec`` is set to the eagerly
evaluated result:

.. code-block:: python

value = 42
precision = 2
template = t"Value: {value:.{precision}f}"
assert template.args[1].format_spec == ".2f"

If no format specification is provided, ``format_spec`` defaults to an empty
string (``""``). This matches the ``format_spec`` parameter of Python's
`format() <https://docs.python.org/3/library/functions.html#format>`_ built-in.
Expand Down Expand Up @@ -273,8 +284,8 @@ a ``SyntaxError``:
Support for the Debug Specifier
-------------------------------

The debug specifier, ``=``, is supported in template strings but behaves
slightly differently than in f-strings. The specifier
The debug specifier, ``=``, is supported in template strings and behaves similarly
to how it behaves in f-strings. The specifier
was introduced in `gh-80998 <https://github.com/python/cpython/issues/80998>`_
outside of any PEP. The distinction in behavior is due to technical limitations
of the implementation.
Expand Down Expand Up @@ -1022,9 +1033,9 @@ source text:

.. code-block:: python

spec = ".2f"
value = 42
template = t"{value:{spec}}"
precision = 2
template = t"Value: {value:.{precision}f}"
assert template.args[1].format_spec == ".2f"

We do not anticipate that these limitations will be a significant issue in practice.
Expand Down