Awaitable typeguards
未关闭
还没有人认领这个 Issue。
topic: feature
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
Currently it seems impossible to have async TypeGuard or TypeIs:
from typing_extensions import TypeIs
async def foo(a: int | str) -> TypeIs[int]:
raise NotImplementedError
async def main() -> None:
a: int | str = 10
if await foo(a):
reveal_type(a)
else:
reveal_type(a)
Current specification is not clear about how async functions returning TypeIs or TypeGuards should be treated, but I believe this would be useful for async lazy evaluation of typestate fields, example:
from typing_extensions import TypeIs, TypeVar, Generic
T = TypeVar('T', int | None, int, None, covariant=True)
class A(Generic[T]):
_a: T
async def get_a(self: A[int | None]) -> int | None:
a = 123 # imagine some expensive async operation, e.g. fetch value from API
self._a = a
return self._a
def method_requires_a(self: A[int]) -> None:
raise NotImplementedError
def __init__(self):
self._a = None
async def has_a(a: A[int | None]) -> TypeIs[A[int]]:
return await a.get_a() is not None
async def main(a: A[int | None]):
if await has_a(a):
a.method_requires_a() # currently fails as type was not narrowed
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 没有指出任何仓库文件、测试或实现入口。首先查看 typing 规范对返回 TypeIs 或 TypeGuard 的异步函数的处理,然后确定应如何定义所提议的类型收窄;完成的标准是达成一份共识规范,并提供相应的实现和测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100