Skip to content

Commit fd74044

Browse files
committed
Add _GeneratorContextManagerBase to contextlib
1 parent 2173c44 commit fd74044

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

stdlib/contextlib.pyi

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from typing import (
88
Awaitable,
99
Callable,
1010
ContextManager,
11+
Generator,
1112
Generic,
1213
Iterator,
1314
Optional,
@@ -26,6 +27,8 @@ if sys.version_info >= (3, 7):
2627

2728
_T = TypeVar("_T")
2829
_T_co = TypeVar("_T_co", covariant=True)
30+
_T_contra = TypeVar("_T_contra", contravariant=True)
31+
_V_co = TypeVar("_V_co", covariant=True)
2932
_T_io = TypeVar("_T_io", bound=Optional[IO[str]])
3033
_F = TypeVar("_F", bound=Callable[..., Any])
3134
_P = ParamSpec("_P")
@@ -36,7 +39,22 @@ _CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)
3639
class ContextDecorator:
3740
def __call__(self, func: _F) -> _F: ...
3841

39-
class _GeneratorContextManager(AbstractContextManager[_T_co], ContextDecorator): ...
42+
if sys.version_info >= (3, 7):
43+
class _GeneratorContextManagerBase(Generic[_P, _T_co, _T_contra, _V_co]): # type: ignore[misc]
44+
def __init__(self, func: Callable[_P, Generator[_T_co, _T_contra, _V_co]], args: _P.args, **kwds: _P.kwargs) -> None: ...
45+
gen: Generator[_T_co, _T_contra, _V_co]
46+
func: Callable[_P, Generator[_T_co, _T_contra, _V_co]] # type: ignore[misc]
47+
args: _P.args
48+
kwds: _P.kwargs
49+
class _GeneratorContextManager( # type: ignore[misc]
50+
_GeneratorContextManagerBase[_P, _T_co, _T_contra, _V_co], # type: ignore[misc]
51+
AbstractContextManager[_T_co],
52+
ContextDecorator,
53+
Generic[_P, _T_co, _T_contra, _V_co], # type: ignore[misc]
54+
): ...
55+
56+
else:
57+
class _GeneratorContextManager(AbstractContextManager[_T_co], ContextDecorator): ...
4058

4159
# type ignore to deal with incomplete ParamSpec support in mypy
4260
def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ... # type: ignore[misc]
@@ -45,7 +63,12 @@ if sys.version_info >= (3, 10):
4563
_AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]])
4664
class AsyncContextDecorator:
4765
def __call__(self, func: _AF) -> _AF: ...
48-
class _AsyncGeneratorContextManager(AbstractAsyncContextManager[_T_co], AsyncContextDecorator): ...
66+
class _AsyncGeneratorContextManager(
67+
_GeneratorContextManagerBase[_P, _T_co, _T_contra, _V_co],
68+
AbstractAsyncContextManager[_T_co],
69+
AsyncContextDecorator,
70+
Generic[_P, _T_co, _T_contra, _V_co],
71+
): ...
4972

5073
elif sys.version_info >= (3, 7):
5174
class _AsyncGeneratorContextManager(AbstractAsyncContextManager[_T_co]): ...

0 commit comments

Comments
 (0)