Wrong Signature for singledispatch'ed functions/methods
还没有人认领这个 Issue。
- 主要语言
- 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用 functools.singledispatch 和 inspect.signature 重现该示例,包括报告中展示的位置参数行为。确定预期的签名语义,然后添加回归覆盖以演示达成一致的结果;当 singledispatch 签名不再误导性地仅表示基础实现时,该 issue 即完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100