From c7ca24ab2447d033a8048b893723c4e6c50d5ba1 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 13 May 2025 14:56:11 +0300 Subject: [PATCH] Add `string.templatelib` in 3.14 (#14044) --- stdlib/@tests/stubtest_allowlists/py314.txt | 1 - stdlib/VERSIONS | 1 + stdlib/{string.pyi => string/__init__.pyi} | 0 stdlib/string/templatelib.pyi | 28 +++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) rename stdlib/{string.pyi => string/__init__.pyi} (100%) create mode 100644 stdlib/string/templatelib.pyi diff --git a/stdlib/@tests/stubtest_allowlists/py314.txt b/stdlib/@tests/stubtest_allowlists/py314.txt index 9e4eb2142..055cae41b 100644 --- a/stdlib/@tests/stubtest_allowlists/py314.txt +++ b/stdlib/@tests/stubtest_allowlists/py314.txt @@ -111,7 +111,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 diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index d13340ab3..1ecd8af64 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -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- diff --git a/stdlib/string.pyi b/stdlib/string/__init__.pyi similarity index 100% rename from stdlib/string.pyi rename to stdlib/string/__init__.pyi diff --git a/stdlib/string/templatelib.pyi b/stdlib/string/templatelib.pyi new file mode 100644 index 000000000..01b95377a --- /dev/null +++ b/stdlib/string/templatelib.pyi @@ -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 + expression: str + conversion: Literal["a", "r", "s"] | None + 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: ...