Subtle pattern in the __subclasshook__ docs isn't explained
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
描述
Documentation
The docs for subclasshook[0] contains the following example:
from abc import ABC
class MyIterable(ABC):
...
@classmethod
def __subclasshook__(cls, C):
if cls is MyIterable:
if any("__iter__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
The docs state that "This method should return True, False or NotImplemented" but the example given only returns True | NotImplemented. This makes it hard for the user to understand how to use the method themselves.
In particular it's not clear to the reader:
- Why they may want to return
NotImplementedinstead ofFalse(this is a common pattern used in stdlib ABCs). - That returning
Falsein__subclasshook__will override the registration of a class due to theissubclassalgorithm. [1][2]
[0] https://docs.python.org/3.12/library/abc.html#abc.ABCMeta.__subclasshook__
[1] Demonstration of this:
from abc import ABC
class Iterable(ABC):
@classmethod
def __subclasshook__(cls, subclass):
return hasattr(subclass, "__iter__")
class NotIterable:
"""Does not define __iter__."""
Iterable.register(NotIterable)
assert issubclass(NotIterable, Iterable)
# 'AssertionErrror'
[2] My understanding of the issubclass algorithm is described in this comment: https://github.com/python/cpython/issues/61035#issuecomment-1361180346
Linked PRs
- gh-100453
- gh-100503
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中链接的 abc.ABCMeta.subclasshook 文档开始,并查看所引用的 issubclass 讨论。将现有示例与 True、False 和 NotImplemented 的所述行为进行比较,然后更新说明并添加或修改示例,以明确其中的区别。开始之前检查链接的 PR gh-100453 和 gh-100503。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100