Skip to content

Add string.templatelib in 3.14 #14044

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

Merged
merged 3 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion stdlib/@tests/stubtest_allowlists/py314.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ sre_compile.CH_NEGATE
sre_constants.CH_NEGATE
sre_parse.CH_NEGATE
string.Template.flags
string.templatelib
sys.is_remote_debug_enabled
sys.remote_exec
tarfile.TarFile.zstopen
Expand Down
1 change: 1 addition & 0 deletions stdlib/VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ ssl: 3.0-
stat: 3.0-
statistics: 3.4-
string: 3.0-
string.templatelib: 3.14-
stringprep: 3.0-
struct: 3.0-
subprocess: 3.0-
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions stdlib/string/templatelib.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from collections.abc import Iterator
from typing import Any, Literal, final

__all__ = ["Interpolation", "Template"]

@final
class Template: # TODO: consider making `Template` generic on `TypeVarTuple`
strings: tuple[str, ...]
interpolations: tuple[Interpolation, ...]

def __new__(cls, *args: str | Interpolation) -> Template: ...
def __iter__(self) -> Iterator[str | Interpolation]: ...
def __add__(self, other: Template | str) -> Template: ...
@property
def values(self) -> tuple[Any, ...]: ... # Tuple of interpolation values, which can have any type

@final
class Interpolation:
value: Any # TODO: consider making `Interpolation` generic in runtime
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also consider making Template generic over a TypeVarTuple. I think being able to have values return the proper types is quite valuable. (Of course, this needs some special coding by type checkers, but template strings will need that anyway.)

expression: str
conversion: Literal["a", "r", "s"] | None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mini rant: Why they used these arcane letters instead of just "repr", "str", and "ascii" is beyond me.

format_spec: str

__match_args__ = ("value", "expression", "conversion", "format_spec")

def __new__(
cls, value: Any, expression: str, conversion: Literal["a", "r", "s"] | None = None, format_spec: str = ""
) -> Interpolation: ...