Need type hint for `getattr`
オープン
まだ誰も着手していません。
topic: feature
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
説明
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
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供されている CallBatch と Callbacks の例から始め、CallBatch.getattr と、要求されている getattr の汎用的な動作に焦点を当てます。issue に記載されているとおり、get_x と get_y の呼び出しで、アクセスした属性名に応じて変化する推論された callback 型が示されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100