mypy suggests `Iterator[Never]`, wants `Iterator[None]` as return type of contextmanager
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
A superficial take on a context manager (https://mypy-play.net/?mypy=latest&python=3.12&gist=68fae9f1b14467dc3a586f2473c9c419):
from contextlib import contextmanager
@contextmanager
def foo() -> None:
yield
results in:
main.py:4: error: Argument 1 to "contextmanager" has incompatible type "Callable[[], None]"; expected "Callable[[], Iterator[Never]]" [arg-type]
main.py:5: error: The return type of a generator function should be "Generator" or one of its supertypes [misc]
If I specify Iterator[Never] as suggested, it still doesn't work (https://mypy-play.net/?mypy=latest&python=3.12&gist=d644d5c880c5f04b0c54941a7cc8c4b2):
from contextlib import contextmanager
from typing import Iterator, Never
@contextmanager
def foo() -> Iterator[Never]:
yield
results in:
main.py:7: error: Yield value expected [misc]
Inspired by https://github.com/python/mypy/issues/3551 I switched it to None (https://mypy-play.net/?mypy=latest&python=3.12&gist=e825ee207b3fbcad373163f5618f467e):
from contextlib import contextmanager
from typing import Iterator, Never
@contextmanager
def foo() -> Iterator[None]:
yield
And now it works:
Success: no issues found in 1 source file
How? Wasn't Never (as in "never returns") supposed to not ask for a return value?
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
Reproduziere die drei Snippets anhand der verlinkten mypy-play-Beispiele und verfolge anschließend, wie mypy Rückgabeannotationen von Generatoren und contextlib.contextmanager behandelt. Als abgeschlossen gilt die Aufgabe, wenn die Diagnosen die gültige Annotation Iterator[None] korrekt erklären und nicht mehr eine unbrauchbare Annotation Iterator[Never] vorschlagen.
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
- 35/100