Skip to content

Commit bfcf8df

Browse files
authored
gh-15769: Make string.templatelib.Interpolation generic (#15774)
Make string.templatelib.Interpolation generic Allows `Interpolation` instances constructed with `__new__` to be parameterized. Instances constructed implicitly via t-strings are unaffected.
1 parent bbb46e7 commit bfcf8df

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

stdlib/string/templatelib.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
from collections.abc import Iterator
22
from types import GenericAlias
3-
from typing import Any, Literal, TypeVar, final, overload
3+
from typing import Any, Generic, Literal, TypeVar, final, overload
44

55
_T = TypeVar("_T")
66

77
@final
88
class Template: # TODO: consider making `Template` generic on `TypeVarTuple`
99
strings: tuple[str, ...]
10-
interpolations: tuple[Interpolation, ...]
10+
interpolations: tuple[Interpolation[Any], ...]
1111

12-
def __new__(cls, *args: str | Interpolation) -> Template: ...
13-
def __iter__(self) -> Iterator[str | Interpolation]: ...
12+
def __new__(cls, *args: str | Interpolation[Any]) -> Template: ...
13+
def __iter__(self) -> Iterator[str | Interpolation[Any]]: ...
1414
def __add__(self, other: Template, /) -> Template: ...
1515
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
1616
@property
1717
def values(self) -> tuple[Any, ...]: ... # Tuple of interpolation values, which can have any type
1818

1919
@final
20-
class Interpolation:
21-
value: Any # TODO: consider making `Interpolation` generic in runtime
20+
class Interpolation(Generic[_T]):
21+
value: _T
2222
expression: str
2323
conversion: Literal["a", "r", "s"] | None
2424
format_spec: str
2525

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

2828
def __new__(
29-
cls, value: Any, expression: str = "", conversion: Literal["a", "r", "s"] | None = None, format_spec: str = ""
30-
) -> Interpolation: ...
29+
cls, value: _T, expression: str = "", conversion: Literal["a", "r", "s"] | None = None, format_spec: str = ""
30+
) -> Interpolation[_T]: ...
3131
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
3232

3333
@overload

0 commit comments

Comments
 (0)