python / python/mypy

Do not raise `unused-awaitable` when it's already `await`ed

Open
#14,795 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-async
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The payload names no files, tests, or entry points. Start by tracing where mypy emits the unused-awaitable diagnostic and how it handles await nodes, then add a regression test based on the examples; done means awaited awaitables no longer produce the diagnostic while unused awaitables still do.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.