- Notifications
You must be signed in to change notification settings - Fork263
Open
Description
from __future__importannotationsfromtypingimportCallable,Generator,LiteralString,Self,TupleclassCallbacks[**P,T]:def__init__(self,funcs:Generator[Callable[P,T]]):self.funcs=funcsdef__call__(self,*args:P.args,**kwargs:P.kwargs):return [func(*args,**kwargs)forfuncinself.funcs]classCallBatch[T]:def__init__(self,objs:Generator[T]):self.objs=objsdef__getattr__[ITEM:LiteralString](self,item:ITEM):returnCallbacks(getattr(obj,item)forobjinself.objs)classA:instances_pool=dict[str,Self]()@classmethoddefget(cls,x:str,y:int):id=cls.get_id(x,y)ifidincls.instances_pool:returncls.instances_pool[id]returncls(x,y)@classmethoddefget_id(cls,x:str,y:int):returnf"{x}_{y}"@classmethoddefcall_batch(cls,*xy:Tuple[str,int]):returnCallBatch[Self](cls.get(x,y)forx,yinxy)def__init__(self,x:str,y:int):self.x=xself.y=yself.id=self.get_id(x,y)self.instances_pool[self.id]=selfdefget_x(self):returnself.xdefget_y(self):returnself.yx=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__
/item
can be marked as type of attributes types ofT
(may be a LiteralString)getattr
should have two generic types, one is type of object, the other is type of input name, and change return type when inputname
changes