Skip to content

Commit 15f6b0a

Browse files
[contextlib] Deprecate @(async)contextmanager wrapping a function returning Iterator (#12087)
1 parent 5c49b1f commit 15f6b0a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

stdlib/contextlib.pyi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from abc import ABC, abstractmethod
55
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
66
from types import TracebackType
77
from typing import Any, Generic, Protocol, TypeVar, overload, runtime_checkable, type_check_only
8-
from typing_extensions import ParamSpec, Self, TypeAlias
8+
from typing_extensions import ParamSpec, Self, TypeAlias, deprecated
99

1010
__all__ = [
1111
"contextmanager",
@@ -86,6 +86,12 @@ class _GeneratorContextManager(
8686
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
8787
) -> bool | None: ...
8888

89+
@overload
90+
def contextmanager(func: Callable[_P, Generator[_T_co, None, object]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
91+
@overload
92+
@deprecated(
93+
"Annotating the return type as `-> Iterator[Foo]` with `@contextmanager` is deprecated. Use `-> Generator[Foo]` instead."
94+
)
8995
def contextmanager(func: Callable[_P, Iterator[_T_co]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
9096

9197
if sys.version_info >= (3, 10):
@@ -112,6 +118,13 @@ else:
112118
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
113119
) -> bool | None: ...
114120

121+
@overload
122+
def asynccontextmanager(func: Callable[_P, AsyncGenerator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
123+
@overload
124+
@deprecated(
125+
"Annotating the return type as `-> AsyncIterator[Foo]` with `@asynccontextmanager` is deprecated. "
126+
"Use `-> AsyncGenerator[Foo]` instead."
127+
)
115128
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
116129
@type_check_only
117130
class _SupportsClose(Protocol):

0 commit comments

Comments
 (0)