@overload doesn't work with @asynccontextmanager
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führen Sie zunächst die bereitgestellte test.py-Reproduktion mit mypy aus und vergleichen Sie die Fälle asynccontextmanager und contextmanager. Verfolgen Sie die Typprüfung für diese Dekoratoren, fügen Sie dann einen Regressionstest hinzu, der überladene asynccontextmanager-Funktionen abdeckt, und stellen Sie sicher, dass mypy für das korrigierte Verhalten keine Fehler meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100