python / python/typing

Merging/Modifying Types

Ouverte
#1,490 3 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

topic: feature
Langage dominant
Python
Étoiles
1.8k
Forks
302
Merge moyen
23 h
PR mergées (30 j)
8

Description

I have a decorator that modifies a class at runtime, kind of like a dataclass:

@modify
class Something:
    ...

Let's say modify adds foo onto Something, so after calling modify you could use it like so:

@modify
class Something
    ...

bar = Something.foo()

dataclass_transform doesn't support this kind of behavior at all. So, how would an API like this work?

I don't see why it would be difficult to implement some sort of Merge type, that takes in a type and "applies"" a Protocol to it. For the above example, it would work like this:

T = TypeVar("T")

class FooProtocol(Protocol):
    @classmethod
    def foo() -> None:
        ...

def modify(tp: type[T]) -> Merge[T, FooProtocol]:
    tp.foo = ...  # type: ignore
    return tp

@modify
class Something:
    ...

bar = Something.foo()

In the above example, ideally it would just sort of mark that the T is now compatible with FooProtocol, but a type: ignore is used inside of modify because it would probably be a mess to try and come up with some sort of API for actually converting a T to Merge[T, ...].

TL;DR A type that marks another type as being compatible with a protocol could be handy for making dataclass-like decorators.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

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 le type Merge proposé et la limitation de dataclass_transform décrite dans l’issue. Déterminez si la spécification de typing dispose d’un emplacement approprié pour ce comportement et quelles sémantiques de compatibilité et de décorateur devraient être définies ; l’issue ne fournit ni fichiers, ni tests, ni point d’entrée concret pour l’implémentation.

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

Évaluation

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

Recevez les nouvelles issues par e-mail

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