[contextlib] Deprecate @(async)contextmanager wrapping a function returning Iterator (#12087)

This commit is contained in:
Thanos
2026-01-23 06:06:18 -05:00
committed by GitHub
parent 5c49b1fe49
commit 15f6b0ac15
+14 -1
View File
@@ -5,7 +5,7 @@ from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
from types import TracebackType
from typing import Any, Generic, Protocol, TypeVar, overload, runtime_checkable, type_check_only
from typing_extensions import ParamSpec, Self, TypeAlias
from typing_extensions import ParamSpec, Self, TypeAlias, deprecated
__all__ = [
"contextmanager",
@@ -86,6 +86,12 @@ class _GeneratorContextManager(
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> bool | None: ...
@overload
def contextmanager(func: Callable[_P, Generator[_T_co, None, object]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
@overload
@deprecated(
"Annotating the return type as `-> Iterator[Foo]` with `@contextmanager` is deprecated. Use `-> Generator[Foo]` instead."
)
def contextmanager(func: Callable[_P, Iterator[_T_co]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
if sys.version_info >= (3, 10):
@@ -112,6 +118,13 @@ else:
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> bool | None: ...
@overload
def asynccontextmanager(func: Callable[_P, AsyncGenerator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
@overload
@deprecated(
"Annotating the return type as `-> AsyncIterator[Foo]` with `@asynccontextmanager` is deprecated. "
"Use `-> AsyncGenerator[Foo]` instead."
)
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
@type_check_only
class _SupportsClose(Protocol):