@overload doesn't work with @asynccontextmanager
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When trying to annotate overloads for an asynccontextmanager, I think mypy incorrectly reports an invalid return type.
To Reproduce
from contextlib import asynccontextmanager, contextmanager
from typing import AsyncIterator, Iterator, overload
@overload
@asynccontextmanager
async def test_async() -> AsyncIterator[int]: ...
@overload
@asynccontextmanager
async def test_async(val: int = ...) -> AsyncIterator[int]: ...
@asynccontextmanager
async def test_async(val: int = 1) -> AsyncIterator[int]:
yield val
@overload
@contextmanager
def test_sync() -> Iterator[int]: ...
@overload
@contextmanager
def test_sync(val: int = ...) -> Iterator[int]: ...
@contextmanager
def test_sync(val: int = 1) -> Iterator[int]:
yield val
This gives an error of "Argument 1 to "asynccontextmanager" has incompatible type "Callable[[int], Coroutine[Any, Any, AsyncIterator[int]]]"; expected "Callable[[int], AsyncIterator[Never]]"
Where the synchronous version checks fine.
Expected Behavior
I expect the async version to work the same as the sync version
Actual Behavior
Mypy reports and error
mypy test.py
test.py:5: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[], Coroutine[Any, Any, AsyncIterator[int]]]"; expected "Callable[[], AsyncIterator[Never]]" [arg-type]
test.py:10: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[int], Coroutine[Any, Any, AsyncIterator[int]]]"; expected "Callable[[int], AsyncIterator[Never]]" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.10.0
- Mypy command-line flags:
mypy test.py - Mypy configuration options from
mypy.ini(and other config files): - Python version used: Python 3.11.4
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo la riproduzione fornita in test.py con mypy e confronta i casi asynccontextmanager e contextmanager. Traccia il controllo dei tipi per questi decoratori, quindi aggiungi un test di regressione che copra le funzioni asynccontextmanager sovraccariche e verifica che mypy non segnali errori per il comportamento corretto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100