Inconsistent behavior when validating type parameter substitutions
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
There are currently three different cases (two of them being closely related) where type parameters can be substituted/parameterized with concrete types:
-
On a user-defined generic class:
class MyGeneric[T1, T2]: ... alias = MyGeneric[int, str] -
On an already parameterized alias (following the previous example):
temp_alias = MyGeneric[int, T2] alias = temp_alias[str] -
On a PEP 695 type alias:
type MyAlias[T1, T2] = dict[T1, T2] gen_alias = MyAlias[int, str]
All of these three cases have slightly different behavior. The one that seems to be the most accurate is the second one. As alias is a _GenericAlias instance, parameterizing it will call _GenericAlias.__getitem__. There is a bunch of logic in there, and it seems that both __typing_prepare_subst__ and then __typing_subst__ (which is only doing type check assertions) are being called.
However, the first case is not making the calls to __typing_subst__, meaning the following would unexpectedly work:
class A[T, **P]: ...
A[int, str]
# ok at runtime, should fail as `P` should be substituted with a valid parameter expression
# (another ParamSpec, the ellipsis, a list/tuple of types or a Concatenate form).
If you do the same on a _GenericAlias instance (matches the second case), an error is raised:
alias = A[T, P]
alias[int, str]
# TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'str'>
This leads us to the first point: should we apply the same logic between these two cases? To avoid breaking changes, we might have to consider forward references:
class A[T, **P]: ...
A[int, 'ForwardParamSpec']
ForwardParamSpec = ParamSpec('ForwardParamSpec')
So perhaps we can exclude ForwardRef from the type check in ParamSpec.__typing_subst__ (note that it already doesn't work for the second case).
I'll also note that having __typing_subst__ not called is not the only difference. For instance, the second case also calls _unpack_args() on the passed args, while the first case doesn't [^1]
Onto the last case (PEP 695 type aliases), currently no validation is performed whatsoever:
type MyAlias[T1, T2] = dict[T1, T2]
MyAlias[int] # no error
So the second point is: should we apply the same logic as in case 1/2? Again, I don't know if applying the same logic here on type aliases is going to introduce any breaking changes concerns?
[^1]: Here is an example of how this manifests: https://gist.github.com/Viicos/db10da58914809a87c0a82c1b2f19162
CPython versions tested on:
3.14
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Lib/typing.py 中的 _GenericAlias.getitem、ParamSpec.typing_subst 和 _unpack_args() 开始,然后将这些路径与直接泛型类和 PEP 695 类型别名的替换进行比较。使用报告中的示例确定验证是否应保持一致,包括前向引用,并验证这三种情况下产生的行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100