Need type hint for `getattr`
Ouverte
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
from __future__ import annotations
from typing import Callable, Generator, LiteralString, Self, Tuple
class Callbacks[**P, T]:
def __init__(self, funcs: Generator[Callable[P, T]]):
self.funcs = funcs
def __call__(self, *args: P.args, **kwargs: P.kwargs):
return [func(*args, **kwargs) for func in self.funcs]
class CallBatch[T]:
def __init__(self, objs: Generator[T]):
self.objs = objs
def __getattr__[ITEM:LiteralString](self, item:ITEM):
return Callbacks(getattr(obj, item) for obj in self.objs)
class A:
instances_pool = dict[str, Self]()
@classmethod
def get(cls, x: str, y: int):
id = cls.get_id(x, y)
if id in cls.instances_pool:
return cls.instances_pool[id]
return cls(x, y)
@classmethod
def get_id(cls, x: str, y: int):
return f"{x}_{y}"
@classmethod
def call_batch(cls, *xy: Tuple[str, int]):
return CallBatch[Self](cls.get(x, y) for x, y in xy)
def __init__(self, x: str, y: int):
self.x = x
self.y = y
self.id = self.get_id(x, y)
self.instances_pool[self.id] = self
def get_x(self):
return self.x
def get_y(self):
return self.y
x = A.call_batch(("1", 1), ("2", 2)).get_x()
x = A.call_batch(("1", 1), ("2", 2)).get_y()
I want type inference for Callbacks.
To achieve this, abilities blow should be support:
CallBatch/__getattr__/itemcan be marked as type of attributes types ofT(may be a LiteralString)getattrshould have two generic types, one is type of object, the other is type of input name, and change return type when inputnamechanges
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 l’exemple fourni de CallBatch et Callbacks, en vous concentrant sur CallBatch.getattr et sur le comportement générique demandé de getattr. Le travail est terminé lorsque les appels get_x et get_y démontrent des types de callback inférés qui varient selon le nom de l’attribut auquel on accède, comme décrit dans l’issue.
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
- 25/100