Need type hint for `getattr`

Aperta
#2,006 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

Inizia con l’esempio fornito di CallBatch e Callbacks, concentrandoti su CallBatch.getattr e sul comportamento generico richiesto di getattr. Il lavoro è completato quando le chiamate get_x e get_y dimostrano tipi di callback inferiti che variano in base al nome dell’attributo a cui si accede, come descritto nell’issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

topic: feature
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()

Image
I want type inference for Callbacks.
To achieve this, abilities blow should be support:

  1. CallBatch/__getattr__/item can be marked as type of attributes types of T(may be a LiteralString)
  2. getattr should have two generic types, one is type of object, the other is type of input name, and change return type when input name changes
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/typing

Tutte le issue di python/typing

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.