python / python/typing

a way to refer to result type of an overloaded function

オープン
#641 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

説明

I am looking for a way to refer to result type of an overloaded function.

To illustrate:

import typing as tp


T = tp.TypeVar("T")


@tp.overload
def gf1(i: int, j: tp.Any) -> str:
    ...


@tp.overload
def gf1(i: tp.Iterable[T], j: bool) -> tp.Dict[T, T]:
    ...


def gf1(i: tp.Union[int, tp.Iterable[T]], j: tp.Any) -> tp.Union[str, tp.Dict[T, T]]:
    pass


gf1.resultType = lambda *a: None



class C(tp.Generic[T]):
    def f1(self) -> gf1.resultType(T, bool):
        pass

    def f2(self) -> gf1.resultType(tp.List[T], bool):
        pass


if not tp.TYPE_CHECKING:
    def reveal_type(x):
        pass


# outputs: builtins.str
reveal_type(gf1(1, True))
# wanted: builtins.str
reveal_type(C[int]().f1())

# outputs: builtins.dict[builtins.int*, builtins.int*]
reveal_type(gf1([1], True))
# wanted: builtins.dict[builtins.int*, builtins.int*]
reveal_type(C[int]().f2())

The f.resultOf syntax is, of course, tentative.

Issue #623 seems to suggest that this may be achievable with a clever use of of Protocol but I just cannot twist my head around that.

Upon request I can elaborate on my specific use case for this but I believe it would suffice to say that in real code gf1.resultType would be used multiple times. Specific argument types for gf1 were chosen arbitrarily for the sake of example.

Not sure whether #548 is anyhow related.

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

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

はじめの一歩

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

調査の方向性

この issue にあるオーバーロードされた関数の例から始め、issues #623 と #548 の関連する議論を確認してください。リポジトリのファイルもテストも特定されていません。完了とするには、オーバーロードされた関数の結果型を参照するための、合意済みで実装可能な範囲が必要です。

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

評価

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

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

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