crates/ty_python_semantic/resources/mdtest/diagnostics/unused_awaitable.md
Calling an async def function produces a coroutine that must be awaited.
async def fetch() -> int:
return 42
async def fetch_complex(x) -> int:
return 42
async def main():
fetch() # error: [unused-awaitable]
fetch_complex(lambda: None) # error: [unused-awaitable]
async def fetch() -> int:
return 42
async def main():
await fetch()
async def fetch() -> int:
return 42
async def main():
# TODO: ty should eventually warn about unused coroutines assigned to variables
coro = fetch()
When a coroutine is passed as an argument rather than used as an expression statement, no diagnostic should be emitted.
async def fetch() -> int:
return 42
async def main():
print(fetch())
The lint fires even outside of async def, since the coroutine is still discarded.
async def fetch() -> int:
return 42
fetch() # error: [unused-awaitable]
When every element of a union is awaitable, the lint should fire.
from types import CoroutineType
from typing import Any
def get_coroutine() -> CoroutineType[Any, Any, int] | CoroutineType[Any, Any, str]:
raise NotImplementedError
async def main():
get_coroutine() # error: [unused-awaitable]
When a union contains a non-awaitable element, the lint should not fire.
from types import CoroutineType
from typing import Any
def get_maybe_coroutine() -> CoroutineType[Any, Any, int] | int:
raise NotImplementedError
async def main():
get_maybe_coroutine()
When an intersection type contains an awaitable element, the lint should fire.
from collections.abc import Coroutine
from types import CoroutineType
from ty_extensions import Intersection
class Foo: ...
class Bar: ...
def get_coroutine() -> Intersection[Coroutine[Foo, Foo, Foo], CoroutineType[Bar, Bar, Bar]]:
raise NotImplementedError
async def main():
get_coroutine() # error: [unused-awaitable]
reveal_type and assert_type are not flaggedCalls to reveal_type and assert_type should not trigger this lint, even when their argument is
an awaitable.
from typing_extensions import assert_type
from types import CoroutineType
from typing import Any
async def fetch() -> int:
return 42
async def main():
reveal_type(fetch()) # revealed: CoroutineType[Any, Any, int]
assert_type(fetch(), CoroutineType[Any, Any, int])
Regular non-awaitable expression statements should not trigger this lint.
def compute() -> int:
return 42
def main():
compute()
Any and Unknown types should not trigger the lint.
from typing import Any
def get_any() -> Any:
return None
async def main():
get_any()