-
Notifications
You must be signed in to change notification settings - Fork 285
Open
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
Description
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 narrowedGlinte
Metadata
Metadata
Assignees
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations