Awaitable typeguards
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
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 narrowed
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no repository files, tests, or implementation entry points. Start by reviewing the typing specification's treatment of async functions returning TypeIs or TypeGuard, then determine how the proposed narrowing should be defined; done requires an agreed specification and corresponding implementation and tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100