python / python/cpython

Wrong Signature for singledispatch'ed functions/methods

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

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

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

An example:

$ cat -n spam.py 
     1  from __future__ import annotations
     2  from functools import singledispatch
     3  
     4  @singledispatch
     5  def foo(x: int) -> int:
     6      return x + 1
     7  
     8  @foo.register
     9  def _(x: str) -> str:
    10      return "Hello, " + x
$ python
Python 3.11.1+ (heads/3.11:ba88628808, Jan 14 2023, 08:40:06) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect, spam
>>> inspect.signature(spam.foo)
<Signature (x: 'int') -> 'int'>

I would expect here something like <Signature (x: int | str) -> int | str> (i.e. union of types for returned value, same for argument) or an exception.

I'm not sure that the first option is on the table: more accurately this situation fits to case of multiple function signatures (like we have in the stdlib e.g. for the min/max). But the current signature() output is misleading.

UPD: In fact, the Signature must be like <Signature (x: int | str, /) -> int | str>, because:

>>> spam.foo(x=1)
Traceback (most recent call last):
  ...
TypeError: foo requires at least 1 positional argument

Or we can make singledispatch'ed function to be with a positional-only argument iff argument names for different overloaded implementations are different.

PS: Inspired by https://discuss.python.org/t/signatures-a-call-to-action/23580

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

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

はじめの一歩

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

調査の方向性

functools.singledispatch と inspect.signature を使って例を再現し、報告に示されている位置引数の挙動も含めます。意図されているシグネチャのセマンティクスを決定し、そのうえで合意した結果を示すリグレッションカバレッジを追加します。singledispatch のシグネチャが基底実装だけを誤解を招く形で表さなくなった時点で issue は完了です。

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

評価

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

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

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