python / python/typing

Option to type default default arguments in Callable types - `WithDefault`

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

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

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

説明

At the moment it's only possible to type callables with default arguments by using a Callback protocol . This adds a lot of additional code for just a few default arguments. Moreover it requires that users have at least a basic understanding of Protocols which is one of the more advanced typing concepts. Lastly, a Callback protocol can't be used to type a generic ParamSpec argument.

Proposal

Add a new WithDefault special type which can be used to annotate arguments in callable types.

Examples
from typing import Callable, TypeAlias, WithDefault

def func(a: str, b: int = 0) -> None: ...
def other(a: str, b: int) -> None: ...

def g(f: Callable[[str, WithDefault[int]], None]) -> None:
    f("Hello")  # ok
    f("World", 2)  # ok

g(func)  # ok
g(other)  # error

For generic ParamSpec types

class Job(Generic[_P]):
    def __init__(self, target: Callable[_P, None]) -> None:
        self.target = target

def g(job: Job[str, WithDefault[int]]) -> None:
    job.target("Hello")  # ok
    job.target("Hello", 2)  # ok

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

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

はじめの一歩

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

調査の方向性

提案の Callable と汎用的な ParamSpec の例から始め、次にデフォルト引数が現在どのようにコールバックプロトコルを通じて表現されているかを確認します。WithDefault の型付け動作を定義し、省略された引数の受け入れと必須引数の拒否を含め、機能が完成したと見なす前に両方の例を検証します。

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

評価

技術スタック
python
領域
developer-experience
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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