python / python/mypy

Treat Assigning Coroutine to `_` the Same as Unused Coroutine

Aperta
#20,570 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

Treat assigning a function's coroutine return value to the _ variable the same as an unused coroutine.

Pitch

Mypy attempts to catch the bug of forgetting to await a coroutine by detecting unused coroutines. For example, with mypy 1.19.1:

async def foo() -> int:
    return 42

def bar() -> None:
    foo()  # error: Value of type "Coroutine[Any, Any, int]" must be used  [unused-coroutine]

It's the style convention of some organizations to assign the unused return value of a function to the _ variable. This signals to readers that the return value was intentionally ignored and not a possible bug. However, this then silences the unused-coroutine issue.

async def foo() -> int:
    return 42

def bar() -> None:
    _ = foo()  # no mypy error

I'm not sure if there is precedent of mypy treating _ differently, but I believe that practically this would aim to catch the same category of bugs as unused-coroutine.

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

Inizia individuando la diagnostica esistente di mypy per le coroutine non utilizzate e la gestione delle assegnazioni a _. Segui l’analisi dell’esempio _=foo(), quindi verifica mediante esempi mirati di controllo dei tipi che l’assegnazione di una coroutine a _ produca la stessa diagnostica del lasciarla inutilizzata.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.