Skip to content

Do not raise unused-awaitable when it's already awaited #14795

Open
@GideonBear

Description

@GideonBear

Feature

Example:

from collections.abc import Generator
from typing import Any

from typing_extensions import Self


class MyAwaitable:
    def __await__(self) -> Generator[Any, None, Self]:
        return self.do_something().__await__()

    async def do_something(self) -> Self:
        # await self.do_async_stuff()
        return self


async def _() -> None:
    MyAwaitable()  # (correct) error: Value of type "MyAwaitable" must be used  [unused-awaitable]
    await MyAwaitable()  # (incorrect, already awaited) error: Value of type "MyAwaitable" must be used  [unused-awaitable]
    await (await MyAwaitable())  # (again incorrect) error: Value of type "MyAwaitable" must be used  [unused-awaitable]

Pitch

I've encountered this while creating a class with an async constructor using __await__, like this:

class AsyncClass:
    def __await__(self) -> Generator[Any, None, Self]:
        return self.__ainit__().__await__()

    async def __ainit__(self) -> Self:
        return self

I don't think it's very useful to suggest adding an await when it's already there.

This could be done by checking if the object that's an unused Awaitable is an await node.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions