python / python/mypy

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

Aperta
#14,795 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-async
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Il payload non indica file, test o punti di ingresso. Inizia tracciando dove mypy emette la diagnostica unused-awaitable e come gestisce i nodi await; il lavoro è completo quando gli awaitable sottoposti ad await non producono più la diagnostica, mentre gli awaitable inutilizzati continuano a produrla.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.