python / python/cpython

ENH: improve doctest class item discovery to include members

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

还没有人认领这个 Issue。

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

描述

Feature or enhancement

Proposal:
>>> import numpy as np
>>> import doctest, pprint
>>> finder = doctest.DocTestFinder()
>>> pprint.pprint(finder.find(np.dtype))
[<DocTest dtype from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (11 examples)>,
 <DocTest dtype.__bool__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__class_getitem__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (2 examples)>,
 <DocTest dtype.__eq__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__ge__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__getitem__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__gt__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__hash__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__le__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__len__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__lt__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__mul__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__ne__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__repr__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__rmul__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.__str__ from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (no examples)>,
 <DocTest dtype.newbyteorder from /home/matti/oss/numpy/build-install/usr/lib/python3.11/site-packages/numpy/__init__.py:None (17 examples)>]

But this does not discover many other items in np.dtype.__dict__ since they are members:

>>> pprint.pprint([(v, type(v)) for k, v in np.dtype.__dict__.items() if k not in ('__doc__',) ])
[(<built-in method __new__ of numpy._DTypeMeta object at 0x74f8bcb40920>,
  <class 'builtin_function_or_method'>),
 (<slot wrapper '__repr__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__hash__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__str__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__lt__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__le__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__eq__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__ne__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__gt__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__ge__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__bool__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__len__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__getitem__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__mul__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<slot wrapper '__rmul__' of 'numpy.dtype' objects>,
  <class 'wrapper_descriptor'>),
 (<method '__reduce__' of 'numpy.dtype' objects>, <class 'method_descriptor'>),
 (<method '__setstate__' of 'numpy.dtype' objects>,
  <class 'method_descriptor'>),
 (<method 'newbyteorder' of 'numpy.dtype' objects>,
  <class 'method_descriptor'>),
 (<method '__class_getitem__' of 'numpy.dtype' objects>,
  <class 'classmethod_descriptor'>),
 (<member 'type' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'kind' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'char' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'num' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'byteorder' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'itemsize' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'alignment' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<member 'flags' of 'numpy.dtype' objects>, <class 'member_descriptor'>),
 (<attribute 'subdtype' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'descr' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'str' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'name' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'base' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'shape' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'ndim' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'isbuiltin' of 'numpy.dtype' objects>,
  <class 'getset_descriptor'>),
 (<attribute 'isnative' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'isalignedstruct' of 'numpy.dtype' objects>,
  <class 'getset_descriptor'>),
 (<attribute 'fields' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'metadata' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'names' of 'numpy.dtype' objects>, <class 'getset_descriptor'>),
 (<attribute 'hasobject' of 'numpy.dtype' objects>,
  <class 'getset_descriptor'>)]

I think the problem is here where only certain kinds of items are checked. Adding inspect.isdatadescriptor() I think would fix this.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

numpy/numpy#28002
scipy/scipy_doctest#178

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Lib/doctest.py 约第 1064 行开始,这里是 DocTestFinder 检查要发现哪些类项的位置。将当前的项检查与拟议添加的 inspect.isdatadescriptor() 进行比较,并查看链接的 NumPy 和 SciPy 讨论。完成的标准是 doctest 发现包含相关类成员,同时不破坏现有的发现行为。

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

评估

技术栈
python
领域
testing-qa, tooling
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

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