python / python/mypy

How to inherit argument types from a specific wrapped function

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

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

feature
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

It would be great if there were a way to inherit the (argument) type signatures from one function to another. The use case here is something like:

def foo(
    something: str,
    *args: *requests.get.args,
    **kwargs: **requests.get.kwargs
):
    print(something)
    return requests.get(*args, **kwargs)

I've found https://github.com/python/mypy/issues/2003, though that was closed a while back. I'm not totally clear why that was closed (from the comments I can't work out what the solution was, nor if agreement was reached on an approach). Additionally a lot has changed since then with regards to argument typing -- notably ParamSpec, TypeVarTuple & Unpack now exist, though I'm not sure if any of them actually help here as the wrapped function is static rather than also being part of the call signature.

Ideally I'd like to be able to do this in cases where the original function is annotated, though without needing to change that function's annotations (e.g: because it's in a library), so using e.g: Unpack[TypedDict] would require me to repeat the definitions of the original function's signature, which is partly what I'm trying to avoid by using passthrough *args, **kwargs in the first place.

Aside: is "inherit" the right term for this? I wasn't sure what to search for.

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

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

はじめの一歩

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

調査の方向性

まず、この issue で提案されている構文と、mypy issue #2003 でのこれまでの議論を確認してください。ParamSpec、TypeVarTuple、または Unpack が、requests.get のような静的にラップされた関数からアノテーションを継承することに対応できるかを比較してください。完了とするには、合意された設計と、型チェッカーの動作に関する文書化が必要です。

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

評価

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

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

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