Doctest ignores methods decorated with `functools.singledispatchmethod`
未關閉
還沒有人認領這個 Issue。
stdlib
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
Doctest does not recognize tests in the docstrings of methods decorated with functools.singledispatchmethod. There's 2 causes:
- 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
functools.singledispatchmethoddoes not callfunctools.update_wrapperon 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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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