python / python/typing_extensions
Starred unpack does not equal `Unpack` in Python 3.11
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 583
- 派生
- 146
- 平均合并
- 10 小时 11 分钟
- 30 天内合并 PR
- 5
描述
Equivalence between typing and typing_extensions is something I do not expect, however I think the following example is an exception as the non-equivalence comes as a surprise:
# python 3.11
import typing
from typing_extensions import Unpack, TypeVarTuple, get_type_hints
Ts = TypeVarTuple("Ts")
def foo(*x: *Ts): ...
print(get_type_hints(foo)['x'] == Unpack[Ts]) # <--- False
print(get_type_hints(foo)['x'] == typing.Unpack[Ts]) # <--- True
# or minimal example:
print(next(iter(Ts)) == Unpack[Ts]) # False
Why does this happen?
The 3.11+ backport of TypeVarTuple returns a patched instance tvt = typing.TypeVarTuple, which in turn will unpack to typing.Unpack[Ts] when __iter__ is used. (Unpack is backported until 3.11)
Possible Fixes
-
Likely bad idea: overwrite
tvt.__iter__to returntyping_extensions.Unpack; might cause same problem at other places. -
Add
__eq__totyping_extensions.Unpackto equal withtyping.Unpack. BUT, this will only be valid for the left-hand-side.print(Unpack[Ts] == get_type_hints(foo)['x']) # <--- True print(get_type_hints(foo)['x'] == Unpack[Ts]) # <--- FalseFix for the right-hand-side:
a)typing_extensions._UnpackAliasmust inherit fromtyping._UnpackGenericAliasfor right-hand side priority.
b) addtyping._UnpackGenericAlias.__eq__via monkey-patchSideeffects: All
Unpackand*-unpacks will be equivalent -
do not fix, but add a limitation/warning to the documentation that in 3.11
typing_extensions.Unpack[Ts] != *Ts == typing.Unpack[Ts] # pseudocode
I already have the necessary code for 2.a, but I am not sure what your stance is on this. Should equality be introduced, and if so is subclassing acceptable?
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 src/typing_extensions.py 的第 2473 行附近开始,复现 issue 中的 Python 3.11 相等性示例。审查关于相等性、子类化、monkey-patching 和文档的备选方案,然后确定 maintainers 需要哪种行为;当选定的行为或限制已记录并经过验证时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100