Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Need type hint forgetattr #2006

Open
Open
Labels
topic: featureDiscussions about new features for Python's type annotations
@STofone

Description

@STofone
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()

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 ofT(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 inputname changes

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp