python / python/cpython

Alias that is generic over a ParamSpec cannot be further specialized

未关闭
#124,445 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic-typing type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

A type alias that is generic over a ParamSpec can be specialized with a list of types, and if this list includes a TypeVar, the resulting alias is still generic to type checkers, but it cannot be subscripted again at runtime.

>>> from collections.abc import Callable
>>> type X[**P] = Callable[P, int]
>>> T = TypeVar("T")
>>> X[[T]]
X[[~T]]
>>> X[[T]][str]
Traceback (most recent call last):
  File "<python-input-14>", line 1, in <module>
    X[[T]][str]
    ~~~~~~^^^^^
TypeError: X[[~T]] is not a generic class

Pyright accepts this code, and I think it's right: https://pyright-play.net/?strict=true&code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgGEDFjkAoUSKVATwvSwHMylarQAqPJADUCIFi1FQAvFHEUpMgBQAiUVoCUc7mqgANANoAqCwAUAukvqN4zJGesAaMllS2WJhcrmZqK2viwAJkjAUMAaAB4AXKaiZjioILZ6CSxQuVAgSABuSIwA%2BkZI8Xq5AMRQGmkg1QC0AHxeqCxAA

Reported by @Daraan in https://github.com/python/typing_extensions/pull/449/files#r1771594266.

I believe this could be fixed by making _Py_make_parameters in genericaliasobject.c recurse into lists.

Linked PRs
  • gh-124512

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 genericaliasobject.c 中的 _Py_make_parameters,并复现 issue 中报告的 X[[T]][str] 失败。在开始之前检查链接的 gh-124512 工作。完成的标准是:使用包含 TypeVar 的列表进行专门化的别名可以在运行时再次进行下标操作,而不会出现 TypeError。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。