From 5a8dbfac1c7511fa4026ffeb67a04393b2bd9729 Mon Sep 17 00:00:00 2001 From: Dave Date: Fri, 6 Jun 2025 15:47:34 -0700 Subject: [PATCH 01/13] Initial commit. --- Doc/library/string.rst | 6 ++ Doc/library/string.templatelib.rst | 109 +++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 Doc/library/string.templatelib.rst diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 23e15780075435..6987d5653fdacc 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -972,3 +972,9 @@ Helper functions or ``None``, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise *sep* is used to split and join the words. + + + +.. toctree:: + + string.templatelib.rst diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst new file mode 100644 index 00000000000000..b052084e5e23db --- /dev/null +++ b/Doc/library/string.templatelib.rst @@ -0,0 +1,109 @@ +:mod:`!string.templatelib` --- Templates and Interpolations for t-strings +========================================================================= + +.. module:: string.templatelib + :synopsis: Support for t-string literals. + +**Source code:** :source:`Lib/string/templatelib.py` + +-------------- + + +.. seealso:: + + :ref:`f-strings` -- Format strings (f-strings) + + +.. _templatelib-template: + +Template +-------- + +The :class:`Template` class describes the contents of a template string. + +The most common way to create a new :class:`Template` instance is to use the t-string literal syntax. This syntax is identical to that of :ref:`f-strings`, except that the string is prefixed with a ``t`` instead of an ``f``. For example, the following code creates a :class:`Template` that can be used to format strings: + + >>> name = "World" + >>> greeting = t"Hello {name}!" + >>> print(list(greeting)) + ['Hello ', Interpolation('World'), '!'] + +It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances: + + >>> from string.templatelib import Template, Interpolation + >>> name = "World" + >>> greeting = Template("Hello, ", Interpolation(name), "!") + >>> print(list(greeting)) + ['Hello, ', Interpolation('World'), '!'] + +.. class:: Template(*args) + + Create a new :class:`Template` object. + + :param args: A mix of strings and :class:`Interpolation` instances in any order. + :type args: str | Interpolation + + If two or more consecutive strings are passed, they will be concatenated into a single value in the :attr:`~Template.strings` attribute. For example, the following code creates a :class:`Template` with a single final string: + + >>> from string.templatelib import Template + >>> greeting = Template("Hello ", "World", "!") + >>> print(greeting.strings) + ('Hello World!',) + + If two or more consecutive interpolations are passed, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with a single value in the :attr:`~Template.strings` attribute: + + >>> from string.templatelib import Template, Interpolation + >>> greeting = Template(Interpolation("World"), Interpolation("!")) + >>> print(greeting.strings) + ('',) + + .. attribute:: strings + + A :ref:`tuple ` of the static strings in the template. + + >>> name = "World" + >>> print(t"Hello {name}!".strings) + ('Hello ', '!') + + Empty strings *are* included in the tuple: + + >>> name = "World" + >>> print(t"Hello {name}{name}!".strings) + ('Hello ', '', '!') + + .. attribute:: interpolations: tuple[Interpolation, ...] + + A tuple of the interpolations in the template. + + >>> name = "World" + >>> print(t"Hello {name}!".interpolations) + (Interpolation('World'),) + + + .. attribute:: values: tuple[Any, ...] + + A tuple of all interpolated values in the template. + + >>> name = "World" + >>> print(t"Hello {name}!".values) + ('World',) + + .. method:: __iter__() -> typing.Iterator[str | Interpolation] + + Iterate over the template, yielding each string and :class:`Interpolation` in order. + + >>> name = "World" + >>> print(list(t"Hello {name}!")) + ['Hello ', Interpolation('World'), '!'] + + Empty strings are *not* included in the iteration: + + >>> name = "World" + >>> print(list(t"Hello {name}{name}")) + ['Hello ', Interpolation('World'), Interpolation('World')] + + + + + + From 09a1e9ebc9088c3262d0f5d923fb8585c2e37ec4 Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 10:49:07 -0400 Subject: [PATCH 02/13] Make clear the preceding discussion about t-strings creating an instance of `templatelib.Template`. --- Doc/library/string.templatelib.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index b052084e5e23db..de8c156e6ed947 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -25,6 +25,8 @@ The most common way to create a new :class:`Template` instance is to use the t-s >>> name = "World" >>> greeting = t"Hello {name}!" + >>> type(greeting) + >>> print(list(greeting)) ['Hello ', Interpolation('World'), '!'] From ec44c2b4df3a09c1dbd2f4e08b08dffcb0ae8e09 Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 11:15:44 -0400 Subject: [PATCH 03/13] Get the import order sorted. --- Doc/library/string.templatelib.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index de8c156e6ed947..fabb71c59bb761 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -32,7 +32,7 @@ The most common way to create a new :class:`Template` instance is to use the t-s It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances: - >>> from string.templatelib import Template, Interpolation + >>> from string.templatelib import Interpolation, Template >>> name = "World" >>> greeting = Template("Hello, ", Interpolation(name), "!") >>> print(list(greeting)) @@ -54,7 +54,7 @@ It is also possible to create a :class:`Template` directly, using its constructo If two or more consecutive interpolations are passed, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with a single value in the :attr:`~Template.strings` attribute: - >>> from string.templatelib import Template, Interpolation + >>> from string.templatelib import Interpolation, Template >>> greeting = Template(Interpolation("World"), Interpolation("!")) >>> print(greeting.strings) ('',) From d8904b671d4c12e6f1e1e861071633d3134b5dcf Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 11:24:15 -0400 Subject: [PATCH 04/13] Correct the Interpolation() calls. --- Doc/library/string.templatelib.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index fabb71c59bb761..34128dd823c8ff 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -34,7 +34,7 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> from string.templatelib import Interpolation, Template >>> name = "World" - >>> greeting = Template("Hello, ", Interpolation(name), "!") + >>> greeting = Template("Hello, ", Interpolation(name, "name"), "!") >>> print(list(greeting)) ['Hello, ', Interpolation('World'), '!'] @@ -55,9 +55,9 @@ It is also possible to create a :class:`Template` directly, using its constructo If two or more consecutive interpolations are passed, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with a single value in the :attr:`~Template.strings` attribute: >>> from string.templatelib import Interpolation, Template - >>> greeting = Template(Interpolation("World"), Interpolation("!")) + >>> greeting = Template(Interpolation("World", "name"), Interpolation("!", "punctuation")) >>> print(greeting.strings) - ('',) + ('', '', '') .. attribute:: strings From 550aa6d4bef57fbc4e312692ea44c1ba2e218b86 Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 12:41:40 -0400 Subject: [PATCH 05/13] Add documentation for Interpolation. --- Doc/library/string.templatelib.rst | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index 34128dd823c8ff..fc62fdd4a66bb3 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -104,8 +104,46 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> print(list(t"Hello {name}{name}")) ['Hello ', Interpolation('World'), Interpolation('World')] +.. class:: Interpolation(*args) + Create a new :class:`Interpolation` object. + :param value: The evaluated, in-scope result of the interpolation. + :type value: object + :param expression: The original *text* of the interpolation's Python :ref:`expressions `. + :type expression: str + :param conversion: The optional :ref:`conversion ` to be used, one of r, s, and a,. + :type value: Literal["a", "r", "s"] | None + :param format_spec: An optional, arbitrary string used as the :ref:`format specification ` to present the value. + :type expression: str = "" + + The :class:`Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned. + + >>> name = "World" + >>> template = t"Hello {name}" + >>> template.interpolations[0].value + 'World' + >>> template.interpolations[0].value = "Galaxy" + Traceback (most recent call last): + File "", line 1, in + AttributeError: readonly attribute + + While f-strings and t-strings are largely similar in syntax and expectations, the :attr:`~Interpolation.conversion` and :attr:`~Interpolation.format_spec` behave differently. With f-strings, these are applied to the resulting value automatically. For example, in this ``format_spec``: + + >>> value = 42 + >>> f"Value: {value:.2f}" + 'Value: 42.00' + + With a t-string :class:`!Interpolation`, the template function is expected to apply this to the value: + + >>> value = 42 + >>> template = t"Value: {value:.2f}" + >> template.interpolations[0].value + 42 + + .. property:: __match_args__: (Literal["value"], Literal["expression"], Literal["conversion"], Literal["format_spec"]) + + The allowed positional arguments used by destructuring during structural pattern matching. From a20e058b9433a1cb6374d4bbd235017c6c434ae8 Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 12:56:27 -0400 Subject: [PATCH 06/13] Convert to use Sphinx modifiers for type, returns, and rtype. --- Doc/library/string.templatelib.rst | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index fc62fdd4a66bb3..a99fd813e396b4 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -60,6 +60,7 @@ It is also possible to create a :class:`Template` directly, using its constructo ('', '', '') .. attribute:: strings + :type: tuple[str, ...] A :ref:`tuple ` of the static strings in the template. @@ -73,7 +74,8 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> print(t"Hello {name}{name}!".strings) ('Hello ', '', '!') - .. attribute:: interpolations: tuple[Interpolation, ...] + .. attribute:: interpolations + :type: tuple[Interpolation, ...] A tuple of the interpolations in the template. @@ -82,7 +84,8 @@ It is also possible to create a :class:`Template` directly, using its constructo (Interpolation('World'),) - .. attribute:: values: tuple[Any, ...] + .. attribute:: values + :type: tuple[Any, ...] A tuple of all interpolated values in the template. @@ -90,7 +93,7 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> print(t"Hello {name}!".values) ('World',) - .. method:: __iter__() -> typing.Iterator[str | Interpolation] + .. method:: __iter__() Iterate over the template, yielding each string and :class:`Interpolation` in order. @@ -104,6 +107,9 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> print(list(t"Hello {name}{name}")) ['Hello ', Interpolation('World'), Interpolation('World')] + :returns: An iterable of all the parts in the template. + :rtype: typing.Iterator[str | Interpolation] + .. class:: Interpolation(*args) Create a new :class:`Interpolation` object. @@ -144,6 +150,7 @@ It is also possible to create a :class:`Template` directly, using its constructo >> template.interpolations[0].value 42 - .. property:: __match_args__: (Literal["value"], Literal["expression"], Literal["conversion"], Literal["format_spec"]) + .. property:: __match_args__ - The allowed positional arguments used by destructuring during structural pattern matching. + :returns: A tuple of the attributes to use for structural pattern matching. + :rtype: (Literal["value"], Literal["expression"], Literal["conversion"], Literal["format_spec"]) From 1f30739db47da95e651fa6d901ca58b984ae1cb5 Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 15:09:52 -0400 Subject: [PATCH 07/13] Add an entry to the glossary. --- Doc/glossary.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index c5c7994f1262a9..fad892998480d3 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -1304,6 +1304,11 @@ Glossary See also :term:`borrowed reference`. + t-string + String literals prefixed with ``'t'`` or ``'T'`` are commonly called + "t-strings" which is short for + :ref:`template string literals `. See also :pep:`750`. + text encoding A string in Python is a sequence of Unicode code points (in range ``U+0000``--``U+10FFFF``). To store or transfer a string, it needs to be From d935dd6fff0976f96e540f528cc6ab6d60b4f4bc Mon Sep 17 00:00:00 2001 From: pauleveritt Date: Sun, 8 Jun 2025 16:54:22 -0400 Subject: [PATCH 08/13] Find any occurrences of f-strings that should also mention t-strings. --- Doc/library/string.rst | 7 ++++--- Doc/reference/compound_stmts.rst | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 6987d5653fdacc..614b714c2baf52 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -198,8 +198,9 @@ Format String Syntax The :meth:`str.format` method and the :class:`Formatter` class share the same syntax for format strings (although in the case of :class:`Formatter`, subclasses can define their own format string syntax). The syntax is -related to that of :ref:`formatted string literals `, but it is -less sophisticated and, in particular, does not support arbitrary expressions. +related to that of :ref:`formatted string literals ` and +:ref:`template string literals `, but it is less sophisticated +and, in particular, does not support arbitrary expressions. .. index:: single: {} (curly brackets); in string formatting @@ -306,7 +307,7 @@ Format Specification Mini-Language "Format specifications" are used within replacement fields contained within a format string to define how individual values are presented (see -:ref:`formatstrings` and :ref:`f-strings`). +:ref:`formatstrings`, :ref:`f-strings`, and :ref:`t-strings`). They can also be passed directly to the built-in :func:`format` function. Each formattable type may define how the format specification is to be interpreted. diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index e95fa3a6424e23..a416cbb4cc8eab 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -852,8 +852,8 @@ A literal pattern corresponds to most The rule ``strings`` and the token ``NUMBER`` are defined in the :doc:`standard Python grammar <./grammar>`. Triple-quoted strings are -supported. Raw strings and byte strings are supported. :ref:`f-strings` are -not supported. +supported. Raw strings and byte strings are supported. :ref:`f-strings` +and :ref:`t-strings` are not supported. The forms ``signed_number '+' NUMBER`` and ``signed_number '-' NUMBER`` are for expressing :ref:`complex numbers `; they require a real number From 1e47362b849a51955ff7dcf468dfd6e9953d783f Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 12 Jun 2025 17:31:13 +0000 Subject: [PATCH 09/13] Fix doctests for str.templatelib --- Doc/library/string.templatelib.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index a99fd813e396b4..523a4b721a451e 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -28,7 +28,7 @@ The most common way to create a new :class:`Template` instance is to use the t-s >>> type(greeting) >>> print(list(greeting)) - ['Hello ', Interpolation('World'), '!'] + ['Hello ', Interpolation('World', 'name', None, ''), '!'] It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances: @@ -36,7 +36,7 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> name = "World" >>> greeting = Template("Hello, ", Interpolation(name, "name"), "!") >>> print(list(greeting)) - ['Hello, ', Interpolation('World'), '!'] + ['Hello, ', Interpolation('World', 'name', None, ''), '!'] .. class:: Template(*args) @@ -81,7 +81,7 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> name = "World" >>> print(t"Hello {name}!".interpolations) - (Interpolation('World'),) + (Interpolation('World', 'name', None, ''),) .. attribute:: values @@ -99,13 +99,13 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> name = "World" >>> print(list(t"Hello {name}!")) - ['Hello ', Interpolation('World'), '!'] + ['Hello ', Interpolation('World', 'name', None, ''), '!'] Empty strings are *not* included in the iteration: >>> name = "World" >>> print(list(t"Hello {name}{name}")) - ['Hello ', Interpolation('World'), Interpolation('World')] + ['Hello ', Interpolation('World', 'name', None, ''), Interpolation('World', 'name', None, '')] :returns: An iterable of all the parts in the template. :rtype: typing.Iterator[str | Interpolation] @@ -147,7 +147,7 @@ It is also possible to create a :class:`Template` directly, using its constructo >>> value = 42 >>> template = t"Value: {value:.2f}" - >> template.interpolations[0].value + >>> template.interpolations[0].value 42 .. property:: __match_args__ From 7b660be31cc6ced394e8ce3d6437ae1aab11f9a6 Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 12 Jun 2025 17:59:33 +0000 Subject: [PATCH 10/13] Start writing the t-strings part of lexical analysis --- Doc/reference/lexical_analysis.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst index b22eb4db7945d1..4af02d43f083e3 100644 --- a/Doc/reference/lexical_analysis.rst +++ b/Doc/reference/lexical_analysis.rst @@ -913,6 +913,31 @@ See also :pep:`498` for the proposal that added formatted string literals, and :meth:`str.format`, which uses a related format string mechanism. +.. _t-strings: +.. _template-string-literals: + +t-strings +--------- + +.. versionadded:: 3.14 + +A :dfn:`template string literal` or :dfn:`t-string` is a string literal +that is prefixed with ``'t'`` or ``'T'``. These strings follow the same +syntax and evaluation rules as `formatted string literals `_, with +the following differences: + +- Rather than evaluating to a `str` object, t-strings evaluate to a + `Template` object from the :mod:`string.templatelib` module. + +- Evaluated expressions are *not* formatted using the + :func:`format` protocol; :meth:`~object.__format__` is *not* invoked. Instead, + the expressions are evaluated and a new `Interpolation` object (also from the + :mod:`string.templatelib` module) is created, which contains the evaluated + value of the expression. That `Interpolation` object is found in the containing + `Template`. + + + .. _numbers: Numeric literals From fcd74e64c74492873d42533d45fc5200919a7a80 Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 17 Jun 2025 17:22:47 +0000 Subject: [PATCH 11/13] Fix lint issues --- Doc/library/string.templatelib.rst | 12 ++++++------ Doc/reference/lexical_analysis.rst | 13 +++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index 523a4b721a451e..63e27fcf1e6297 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -19,9 +19,9 @@ Template -------- -The :class:`Template` class describes the contents of a template string. +The :class:`!Template` class describes the contents of a template string. -The most common way to create a new :class:`Template` instance is to use the t-string literal syntax. This syntax is identical to that of :ref:`f-strings`, except that the string is prefixed with a ``t`` instead of an ``f``. For example, the following code creates a :class:`Template` that can be used to format strings: +The most common way to create a new :class:`!Template` instance is to use the t-string literal syntax. This syntax is identical to that of :ref:`f-strings`, except that the string is prefixed with a ``t`` instead of an ``f``. For example, the following code creates a :class:`Template` that can be used to format strings: >>> name = "World" >>> greeting = t"Hello {name}!" @@ -30,7 +30,7 @@ The most common way to create a new :class:`Template` instance is to use the t-s >>> print(list(greeting)) ['Hello ', Interpolation('World', 'name', None, ''), '!'] -It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances: +It is also possible to create a :class:`!Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances: >>> from string.templatelib import Interpolation, Template >>> name = "World" @@ -40,7 +40,7 @@ It is also possible to create a :class:`Template` directly, using its constructo .. class:: Template(*args) - Create a new :class:`Template` object. + Create a new :class:`!Template` object. :param args: A mix of strings and :class:`Interpolation` instances in any order. :type args: str | Interpolation @@ -112,7 +112,7 @@ It is also possible to create a :class:`Template` directly, using its constructo .. class:: Interpolation(*args) - Create a new :class:`Interpolation` object. + Create a new :class:`!Interpolation` object. :param value: The evaluated, in-scope result of the interpolation. :type value: object @@ -126,7 +126,7 @@ It is also possible to create a :class:`Template` directly, using its constructo :param format_spec: An optional, arbitrary string used as the :ref:`format specification ` to present the value. :type expression: str = "" - The :class:`Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned. + The :class:`!Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned. >>> name = "World" >>> template = t"Hello {name}" diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst index 4af02d43f083e3..f2a8cf9e6afd37 100644 --- a/Doc/reference/lexical_analysis.rst +++ b/Doc/reference/lexical_analysis.rst @@ -926,15 +926,16 @@ that is prefixed with ``'t'`` or ``'T'``. These strings follow the same syntax and evaluation rules as `formatted string literals `_, with the following differences: -- Rather than evaluating to a `str` object, t-strings evaluate to a - `Template` object from the :mod:`string.templatelib` module. +- Rather than evaluating to a ``str`` object, t-strings evaluate to a + :class:`~string.templatelib.Template` object from the + :mod:`string.templatelib` module. - Evaluated expressions are *not* formatted using the :func:`format` protocol; :meth:`~object.__format__` is *not* invoked. Instead, - the expressions are evaluated and a new `Interpolation` object (also from the - :mod:`string.templatelib` module) is created, which contains the evaluated - value of the expression. That `Interpolation` object is found in the containing - `Template`. + the expressions are evaluated and a new :class:`~string.templatelib.Interpolation`` + object (also from the :mod:`string.templatelib` module) is created, which + contains the evaluated value of the expression. That ``Interpolation`` object + is found in the containing ``Template``. From a796f5d3ec34196af677c9431f7b05094e93fe6b Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 17 Jun 2025 17:40:05 +0000 Subject: [PATCH 12/13] Fix further sphinx warnings --- Doc/library/string.templatelib.rst | 32 ++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Doc/library/string.templatelib.rst b/Doc/library/string.templatelib.rst index 63e27fcf1e6297..8b9fed96008e71 100644 --- a/Doc/library/string.templatelib.rst +++ b/Doc/library/string.templatelib.rst @@ -11,7 +11,7 @@ .. seealso:: - :ref:`f-strings` -- Format strings (f-strings) + :ref:`Format strings ` .. _templatelib-template: @@ -124,7 +124,6 @@ It is also possible to create a :class:`!Template` directly, using its construct :type value: Literal["a", "r", "s"] | None :param format_spec: An optional, arbitrary string used as the :ref:`format specification ` to present the value. - :type expression: str = "" The :class:`!Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned. @@ -154,3 +153,32 @@ It is also possible to create a :class:`!Template` directly, using its construct :returns: A tuple of the attributes to use for structural pattern matching. :rtype: (Literal["value"], Literal["expression"], Literal["conversion"], Literal["format_spec"]) + + + .. property:: value + + :returns: The evaluated value of the interpolation. + :rtype: object + + .. property:: expression + + :returns: The original text of the interpolation's Python expression if the interpolation was created from a t-string literal + :rtype: str + + The :attr:`~Interpolation.expression` is the original text of the interpolation's Python expression, if the interpolation was created from a t-string literal. Developers creating + interpolations manually should either set this to an empty + string or choose a suitable valid python expression. + + .. property:: conversion + + :returns: The conversion to apply to the value, one of "a", "r", or "s", or None. + :rtype: Literal["a", "r", "s"] | None + + The :attr:`~Interpolation.conversion` is the optional conversion to apply to the value. This is one of "a", "r", or "s", or None if no conversion is specified. + + .. property:: format_spec + + :returns: The format specification to apply to the value. + :rtype: str + + The :attr:`~Interpolation.format_spec` is an optional, arbitrary string used as the format specification to present the value. This is similar to the format specification used in :ref:`format strings `, but it is not limited to a specific set of formats. From 21d337c7223aaafd4d2b96e5f59e4ada2f4e579d Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 17 Jun 2025 18:02:12 +0000 Subject: [PATCH 13/13] More! --- Doc/reference/lexical_analysis.rst | 13 ++++++------- Doc/sphinx-warnings.txt | 0 2 files changed, 6 insertions(+), 7 deletions(-) create mode 100644 Doc/sphinx-warnings.txt diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst index f2a8cf9e6afd37..a6936b1d36a6de 100644 --- a/Doc/reference/lexical_analysis.rst +++ b/Doc/reference/lexical_analysis.rst @@ -923,20 +923,19 @@ t-strings A :dfn:`template string literal` or :dfn:`t-string` is a string literal that is prefixed with ``'t'`` or ``'T'``. These strings follow the same -syntax and evaluation rules as `formatted string literals `_, with +syntax and evaluation rules as :ref:`formatted string literals `, with the following differences: - Rather than evaluating to a ``str`` object, t-strings evaluate to a :class:`~string.templatelib.Template` object from the :mod:`string.templatelib` module. -- Evaluated expressions are *not* formatted using the - :func:`format` protocol; :meth:`~object.__format__` is *not* invoked. Instead, - the expressions are evaluated and a new :class:`~string.templatelib.Interpolation`` - object (also from the :mod:`string.templatelib` module) is created, which - contains the evaluated value of the expression. That ``Interpolation`` object - is found in the containing ``Template``. +- The :func:`format` protocol is not used. Instead, the format specifier and + conversions (if any) are passed to a new :class:`~string.templatelib.Interpolation` + object that is created for each evaluated expression. +- Format specifiers containing nested replacement fields are evaluated eagerly, + prior to being passed to the :class:`~string.templatelib.Interpolation` object. .. _numbers: diff --git a/Doc/sphinx-warnings.txt b/Doc/sphinx-warnings.txt new file mode 100644 index 00000000000000..e69de29bb2d1d6