crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_await.md
This is a test showcasing a primitive case where an object is not awaitable.
async def main() -> None:
await 1 # error: [invalid-await]
__await__This diagnostic also points to the class definition if available.
class MissingAwait:
pass
async def main() -> None:
await MissingAwait() # error: [invalid-await]
__await__This diagnostic also points to the method definition if available.
from datetime import datetime
class PossiblyUnbound:
if datetime.today().weekday() == 0:
def __await__(self):
yield
async def main() -> None:
await PossiblyUnbound() # error: [invalid-await]
__await__class Awaitable:
def __await__(self):
yield
class NotAwaitable: ...
async def _(flag: bool) -> None:
x = Awaitable() if flag else NotAwaitable()
await x # error: [invalid-await]
__await__ definition with extra argumentsCurrently, the signature of __await__ isn't checked for conformity with the Awaitable protocol
directly. Instead, individual anomalies are reported, such as the following. Here, the diagnostic
reports that the object is not implicitly awaitable, while also pointing at the function parameters.
class InvalidAwaitArgs:
def __await__(self, value: int):
yield value
async def main() -> None:
await InvalidAwaitArgs() # error: [invalid-await]
__await__This diagnostic doesn't point to the attribute definition, but complains about it being possibly not awaitable.
class NonCallableAwait:
__await__ = 42
async def main() -> None:
await NonCallableAwait() # error: [invalid-await]
__await__ definition with explicit invalid return type__await__ must return a valid iterator. This diagnostic also points to the method definition if
available.
class InvalidAwaitReturn:
def __await__(self) -> int:
return 5
async def main() -> None:
await InvalidAwaitReturn() # error: [invalid-await]
When multiple potential definitions of __await__ exist, all of them must be proper in order for an
instance to be awaitable. In this specific case, no specific function definition is highlighted.
import typing
from datetime import datetime
class UnawaitableUnion:
if datetime.today().weekday() == 6:
def __await__(self) -> typing.Generator[typing.Any, None, None]:
yield
else:
def __await__(self) -> int:
return 5
async def main() -> None:
await UnawaitableUnion() # error: [invalid-await]