`singledispatchmethod` inspected signature incorrectly includes `self`
还没有人认领这个 Issue。
评估
调研方向
阅读 Lib/functools.py 第 979 行附近的内容,重点关注 singledispatchmethod.get 如何包装绑定方法。运行提供的 inspect.signature 和 sig.bind 复现;当绑定签名省略 self、接受方法的实际参数,并且相关测试通过时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Bug report
Bug description:
The result of singledispatchmethod.__get__ is wrapped to imitate the underlying function, instead of the result of func.__get__. This leads to an incorrect signature being reported by inspect.signature, which includes the leading self argument, thus misleading introspection tools as to the real arguments the method accepts.
import inspect
from functools import singledispatchmethod
class A:
@singledispatchmethod
def a(self, arg):
pass
sig = inspect.signature(A().a)
# <Signature (self, arg)> but we expect <Signature (arg)>
sig.bind(None) # should be OK because `A().a(None)` is OK. Instead:
# TypeError: missing a required argument: 'arg'
# because the first argument is erroneously bound to `self`.
One way to fix this might be to edit https://github.com/python/cpython/blob/6258844c27e3b5a43816e7c559089a5fe0a47123/Lib/functools.py#L979 to read instead
update_wrapper(_method, self.func.__get__(obj, cls))
CPython versions tested on:
3.10, 3.12
Operating systems tested on:
Linux, Windows
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
fix: inaccuracy ⚠️
难度 2/5 1-3 小时 新手友好度 72/100
uabrc/uabrc.github.io#1255 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
ethereum-optimism/factory#64 ·
-
难度 2/5 1-3 小时 新手友好度 90/100
duckdb/duckdb-python#627 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
-
documentation
难度 1/5 1 小时以内 新手友好度 78/100
Qiskit/qiskit-addon-sqd#376 ·