python / python/cpython

Doctest ignores methods decorated with `functools.singledispatchmethod`

未关闭
#129,578 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

Doctest does not recognize tests in the docstrings of methods decorated with functools.singledispatchmethod. There's 2 causes:

  1. It checks the dictionary of classes for functions and subclasses, thereby not invoking descriptors. https://github.com/python/cpython/blob/df4a2f5bd74fc582d99e6a82e070058d7765f44d/Lib/doctest.py#L1061-L1073
  2. functools.singledispatchmethod does not call functools.update_wrapper on its instance, only on the wrapper function returned from its __get__ method.

A similar issue for cached_property was fixed in #107996.

Example

# mwe.py
import functools

class Class:
    @functools.singledispatchmethod
    def singledispatchmethod(self, arg):
        """
        >>> print(Class().singledispatchmethod(5))
        foo
        """
        return "foo"

    def regularmethod(self, arg):
        """
        >>> print(Class().regularmethod(5))
        foo
        """
        return "foo"
$ python3 -m doctest -v mwe.py
Trying:
    print(Class().regularmethod(5))
Expecting:
    foo
ok
2 items had no tests:
    mwe
    mwe.Class
1 item passed all tests:
   1 test in mwe.Class.regularmethod
1 test in 3 items.
1 passed.
Test passed.

doctest only finds one doctest, where there should be two.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-129579

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Lib/doctest.py 中 1061-1073 行附近的类字典处理开始,然后检查 functools.singledispatchmethod 以及 issue #107996 中引用的 cached_property 修复。为 singledispatchmethod 示例的 docstring 添加回归覆盖,并验证 doctest 能发现这两个方法。

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

评估

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

把新 issue 发到你的邮箱

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