python / python/cpython

`singledispatchmethod` inspected signature incorrectly includes `self`

未关闭 适合新手
#117,735 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 Lib/functools.py 第 979 行附近的内容,重点关注 singledispatchmethod.get 如何包装绑定方法。运行提供的 inspect.signature 和 sig.bind 复现;当绑定签名省略 self、接受方法的实际参数,并且相关测试通过时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
68/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。