Awaitable typeguards
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
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.
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
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
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.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·