python / python/typing

Generic ParamSpec in subclass definitions

オープン
#1,405 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic: feature
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

Given some base class that is generic over a param-spec, I'd like to be able to define the param-spec using a subclass method implementation. Something like:

T = TypeVar("T")
P = ParamSpec("P")

class Base(Generic[P, T]):
    func: Callable[P, T]
    # for instance
    def wrapper(self, *args: P.args, **kwargs: P.kwargs) -> Tuple[T]:
        return (self.func(*args, **kwargs), )
        
class Subclass(Base):
    def func(self, x: int, *, y: str) -> bytes: ...

That is, the base class is generic over some function definition, and the subclass implement that function as a method. I'm not sure how common this pattern is, but I see it a fair amount when the class gets more complicated and you can't just use a decorator (e.g. torch.nn.Module.forward is a big example of this). Is there some other way of specifying "infer the parameters from this method implementation"? Is this something that would require an update to the specification, is it "just" a feature request to the method implementation "like" an assignment statement?

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ファイル、テスト、実装のエントリーポイントは指定されていません。まず、issue の ParamSpec、Generic、サブクラスメソッドの例を Python typing 仕様と併せて確認し、要求された推論がサポートされているのか、それとも仕様の変更が必要なのかを判断してください。動作とその実装範囲が明示的に決定されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。