python / python/mypy

Add ReturnType-like utility for getting function return types

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

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

feature needs discussion priority-2-low topic-usability
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

This is a feature request, so if there is a way to achieve this already, please point me and future me in the right way.

In TypeScript, we can use the ReturnType utility type to get the return type of a function to reuse in other places. This is very useful to make the code short, readable, and easy to change if necessary, especially in cases where I want to wrap the call of a function from another module in some other function.

An example of how it might be used:

# module: foo.py
def foo() -> None | baz.Baz | tuple[int, int]:
    ...

# module: bar.py
def bar() -> ReturnType[foo.foo]:
    ...

In this case, I could maybe give the return type a name like FooReturn and import that in bar, but that seems less elegant rather than just reusing the function I'm already using.


Note that if automatic or manual type inference (https://github.com/python/mypy/issues/4409) was implemented, most of my usages would be covered.

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

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

はじめの一歩

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

調査の方向性

まず、この issue で要求されている ReturnType に似たユーティリティと、自動または手動の型推論に関するリンク先の mypy issue #4409 を確認します。foo.py と bar.py の例に対して意図された型構文と動作を判断します。done は、要求された戻り値型の再利用がサポートされているか、既存のサポート対象のアプローチが文書化されていることを意味するものとします。

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

評価

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

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

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