Spec: More precision on type parameter order
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 55/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- python
调研方向
从 generics 规范的“Arbitrary generic types as base classes”部分开始,尤其关注其中当前的类型变量排序规则。将该措辞与 issue 中针对 PEP 695、Generic、Protocol 和基类情况提出的方案进行比较。当规范明确规定每种情况下的排序,并与给出的示例一致时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
The spec currently isn't very precise on the order in which type parameters appear in a class. (Given a class C that is generic over type parameters T1 and T2, does C[int, str] mean that T1 is bound to int or that T2 is bound to int?)
I think the rule should be:
- If PEP 695 syntax is used, the order is the order in which the type parameters appear in the type parameter list.
- If the class has
Generic[...]as a syntactic base, the order is the order of the type arguments toGeneric. - If the class has
Protocol[...]as a syntactic base, the order is the order of the type arguments toProtocol. This applies only if Protocol is subscripted, not if the class inherits from bareProtocol. - Otherwise, the order is the order in which the type parameters appear syntactically in the base class list.
As far as I know, this is how all type checkers currently behave, but we just found out it's not how the runtime behaves when Protocol is involved: https://github.com/python/cpython/issues/137191#issuecomment-3132748382.
In these examples, all classes are generic over two parameters T1 and T2, and in all cases the type parameter order is T1, T2:
from typing import Generic, Protocol, TypeVar
class C1[T1, T2]: ... # order is T1, T2
T1 = TypeVar("T1")
T2 = TypeVar("T2")
class C2(C1[T2, T1], Generic[T1, T2]): ... # order is T1, T2
class GenericProto[T1, T2](Protocol): ... # order is T1, T2
class Proto2(GenericProto[T2, T1], Protocol[T1, T2]): ... # order is T1, T2
class Proto3(GenericProto[T1, T2], Protocol): ... # order is T1, T2
class C3(C2[T1, T2]): ... # order is T1, T2
We should change the spec to make this rule explicit. The only current rule appears to be in https://typing.python.org/en/latest/spec/generics.html#arbitrary-generic-types-as-base-classes "Type variables are applied to the defined class in the order in which they first appear in any generic base classes".
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/typing 的其他 Issue
-
topic: typing spec
难度 2/5 1-3 小时 新手友好度 72/100
-
topic: typing spec
难度 2/5 1-3 小时 新手友好度 75/100
-
topic: documentation
难度 2/5 1-3 小时 新手友好度 76/100
-
topic: documentation
难度 2/5 1-3 小时 新手友好度 65/100
-
topic: conformance tests topic: typing spec
难度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
triage/confirmed
难度 2/5 1-3 小时 新手友好度 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
难度 1/5 1 小时以内 新手友好度 92/100
NousResearch/hermes-agent#118866 ·
-
bug
难度 1/5 1 小时以内 新手友好度 90/100
apache/cloudstack#14222 ·
-
难度 2/5 1-3 小时 新手友好度 76/100
-
bug
难度 2/5 1-3 小时 新手友好度 82/100