python / python/typeshed

(3.14) Templates not typeable without TypeVarTuple transformations

Ouverte
#14,878 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Python
Étoiles
5.1k
Forks
2.1k
Merge moyen
1 j 19 h
PR mergées (30 j)
82

Description

I recently tried to create my own stubs for Python 3.14's Template and Interpolation types, but it seems that is currently not possible. I waited a while and searched a lot and did not find anybody talk about this issue anywhere.

Their runtime generic-ness was recently added:
https://github.com/python/cpython/issues/133970

But it seems that it is not currently possible to actually type hint them correctly within the current typing spec, because transformations of the TypeVarTuple would be necessary. (https://github.com/python/typing/issues/1216)

Currently the code for those two classes' typing stubs looks like this:

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, /) -> Template: ...
    def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
    @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: ...
    def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...

And it would need to be updated to something like this (using newer syntax for this example):

from types import GenericAlias
from typing import Any, final, Iterator, Literal

class Template[*Ts]:
    strings: tuple[str, ...]
    interpolations: tuple[Interpolation[Ts], ...]  # Not possible to map/transform onto interpolations

    def __new__(cls, *args: str | Interpolation[Ts]) -> Template: ...  # Not possible to map/transform onto interpolations
    def __iter__(self) -> Iterator[str | Interpolation[Ts]]: ...  # Not possible to map/transform onto interpolations
    def __add__[*OtherTs](self, other: Template[*OtherTs], /) -> Template[*Ts, *OtherTs]: ...
    def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
    @property
    def values(self) -> tuple[*Ts]: ...

@final
class Interpolation[T]:
    value: T
    expression: str
    conversion: Literal["a", "r", "s"] | None
    format_spec: str

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

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

So basically we would need to map the TypeVarTuple's elements into each Interpolation, which is currently not possible.

So it looks like typing Templates and Interpolations is not possible until transformations get added. If I missed something or there is a workaround, please inform me! Thank you! 👍

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par examiner les définitions stub de Template et Interpolation dans l’issue, puis lisez l’issue CPython liée ainsi que la discussion typing-spec sur les transformations de TypeVarTuple. Déterminez si la spécification du typage dispose d’un contournement pris en charge ou si elle nécessite une nouvelle capacité ; le travail est terminé lorsqu’il existe une manière convenue et implémentable de typer les deux classes.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
developer-experience
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.