python / python/mypy

Cannot set return type of asynccontextmanagers in Protocol classes

Aperta
#17,996 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

When decorating an empty method ("...") inside of a Protocol definition as an asynccontextmanager, the type checker insists that the return type must use Never instead of allowing to specify the type intended by the protocol.

To Reproduce

See also in Playground.

from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Protocol


class P(Protocol):
    @asynccontextmanager
    async def f(self) -> AsyncIterator[None]:
        """Create a context but yield nothing."""
        ...

Importing AsyncIterator from typing instead of collections.abc makes no difference.

Expected Behavior

I should be allowed to annotate the expected yield-Type of the context manager in a Protocol without having to provide an implementation. (None in this case, but also any other type that the context will yield to the with statement.)

Actual Behavior

Produces this error:

main.py:7: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[P], Coroutine[Any, Any, AsyncIterator[None]]]"; expected "Callable[[P], AsyncIterator[Never]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.12.0
  • Mypy command-line flags: (None)
  • Mypy configuration options from mypy.ini (and other config files): (see below)
  • Python version used: 3.12.4

My mypy config (but it also reproduces when leaving all options unchecked in the Playground):

[tool.mypy]
ignore_missing_imports = false
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_any_unimported = true
strict_optional = true
no_implicit_optional = true
check_untyped_defs = true
show_error_codes = true
warn_unused_configs = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = true
strict_equality = true
strict = true

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

Riproduci la diagnostica con l’esempio Python 3.12 nell’issue o nel Playground collegato, quindi traccia come mypy gestisce @asynccontextmanager su un metodo Protocol vuoto. Il lavoro è completo quando l’esempio accetta AsyncIterator[None] e tipi yield analoghi senza l’errore arg-type segnalato, con un test di regressione che lo copra.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 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.