Allow `NotRequired[]` to be passed as a `TypeVar`
未关闭
还没有人认领这个 Issue。
topic: feature
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
I just found myself wanting to do the following:
from typing import TypedDict, NotRequired, Generic, TypeVar, Union, Never
_T = TypeVar('_T')
Foo = Union['FooSub1[_T]', 'FooSub2[_T]', 'FooSub3[_T]']
class BaseFoo(TypedDict, Generic[_T]):
bar: _T
class FooSub1(BaseFoo[_T]):
sub1: str
class FooSub2(BaseFoo[_T]):
sub2: str
class FooSub3(BaseFoo[_T]):
sub3: str
def foo(foo_with_bar: Foo[int]):
reveal_type(foo_with_bar) # Type of "foo_with_bar" is "FooSub1[int] | FooSub2[int] | FooSub3[int]"
def bar(foo_without_bar: Foo[NotRequired[Never]]): # error: "NotRequired" is not allowed in this context
pass
Mypy and Pyright both disallow NotRequired to be passed as a TypeVar. However the only way to avoid doing that would require me to duplicate all subclass definitions, which I am not willing to do because I have more than a dozen and so much duplication would make the code less maintainable.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中的复现代码开始,比较报告中提到的两个检查器 mypy 和 Pyright 当前对 NotRequired 和 TypeVar 的规则。没有确定任何文件或测试;完成这项工作需要达成一致的类型规则设计以及相应的检查器行为,同时不重复 TypedDict 子类。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100