Merging/Modifying Types
Personne n'a encore pris cette issue.
- 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
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- 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