Missing reflected dunder methods from AbstractSet
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 82
描述
Problem
Reflected operations (__rand__, __ror__, __rsub__, __rxor__) are missing from AbstractSet and its subclasses.
These are noted as explicit exclusions from tests: https://github.com/python/typeshed/blob/9f8f621918e4a187c49d68290a89f5f3dbe0cba9/stdlib/%40tests/stubtest_allowlists/common.txt#L50-L54
Example of consequences
@samueljsb and I have defined a protocol (in another project) for an object which has an __rsub__ method, and discovered that set, despite implementing this method, does not adhere to the protocol.
A simplified illustration of this issue is:
from typing import Any, Protocol
class Subtractable(Protocol):
def __rsub__(self, other: Any) -> Any: ...
_: Subtractable = set()
$ mypy t.py
t.py:6: error: Incompatible types in assignment (expression has type "set[Never]", variable has type "Subtractable") [assignment]
Found 1 error in 1 file (checked 1 source file)
Demonstrated with:
- mypy 1.19.1 (compiled: yes)
- Python 3.14.2 (main, Dec 5 2025, 16:49:16) [Clang 17.0.0 (clang-1700.4.4.1)]
Investigation
The exclusions reference https://github.com/python/typeshed/issues/7414, which was an attempt to add type hints for them. It was closed without merging because it encountered errors. We believe it encountered errors because the type signatures were wrong. Specifically, it didn't account for the return types of the operations matching the inputs.
For example, instead of:
def __rsub__(self, s: AbstractSet[_T]) -> AbstractSet[_T]: ...
We believe the signature needs to be (in the new syntax):
def __rsub__[T_Set: AbstractSet](self, other: T_Set) -> T_Set: ...
The above can be applied to __rsub__ and __rand__, because those methods return a subset of the input, and therefore will not change its type.
__ror__ and __rxor__ are more complicated because they return a type which is a superset of both inputs. We haven't yet looked into how to solve those.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 stdlib/@tests/stubtest_allowlists/common.txt 中的排除项开始,并与 CPython 的 Lib/_collections_abc.py 中的 AbstractSet 实现进行比较。跟踪 AbstractSet 的存根及其子类,确定全部四个反射方法的兼容签名,然后确认在相关存根测试通过后可以移除这些排除项。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100