(🎁) Support type variables in the bounds of other type variables
未关闭
还没有人认领这个 Issue。
topic: feature
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
from typing import _T as T, TypeVar
L = TypeVar("L", bound=list[T]) # error: Type variable "typing._T" is unbound
def foo(l: L) -> L | T: # error: A function returning TypeVar should receive at least one argument containing the same Typevar
res = l[0]
if res:
return res
return l
a: list[int] | list[str]
reveal_type(foo) # "def [L <: list[T?], T] (l: L) -> L | T"
reveal_type(foo(a)) # list[int] | list[str]
Expected:
reveal_type(foo) # "def [T, L <: list[T]] (l: L) -> L | T"
reveal_type(foo(a)) # list[int] | list[str] | int | str
Mypy should understand that T should be bound to foo from L
This is identical to generic TypeAliases:
L: TypeAlias = list[T]
a: L[int]
Here T is unbound, yet it's a valid and semantically sound expression.
Typescript example
The same idea could be represented in TypeScript as:
declare function foo<T, L extends T[]>(l: L): L | T
declare let a: number[] | string[]
let b = foo(a)
Although TS fails to infer the correct type here. (it infers as unknown)
basedmypy
This is partially supported in basedmypy
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中的 TypeVar 和泛型 TypeAlias 示例开始,然后比较当前对 bounds 和推断类型的处理方式。当所示的 reveal_type 输出将 T 视为通过 L 绑定,并为 foo(a) 生成预期的 union 时,工作就完成了。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 28/100