Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43f637b

Browse files
authored
add _GeneratorContextManagerBase.__init__ (#13246)
I think that #6676 showed that Paramspec didn't work, but that wasn't actually the fault of _GeneratorContextManagerBase.
1 parent b29e26a commit 43f637b

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ _collections_abc.AsyncGenerator.ag_running
1111
asyncio.__all__
1212
builtins.dict.get
1313
collections\.ChainMap\.fromkeys # https://github.com/python/mypy/issues/17023
14-
contextlib._GeneratorContextManagerBase.__init__ # skipped in the stubs in favor of its child classes
1514
http.client.HTTPConnection.response_class # the actual type at runtime is abc.ABCMeta
1615
importlib.abc.Loader.exec_module # See Lib/importlib/_abc.py. Might be defined for backwards compatibility
1716
importlib.abc.MetaPathFinder.find_spec # Not defined on the actual class, but expected to exist.

stdlib/contextlib.pyi

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ _T_co = TypeVar("_T_co", covariant=True)
3333
_T_io = TypeVar("_T_io", bound=IO[str] | None)
3434
_ExitT_co = TypeVar("_ExitT_co", covariant=True, bound=bool | None, default=bool | None)
3535
_F = TypeVar("_F", bound=Callable[..., Any])
36+
_G = TypeVar("_G", bound=Generator[Any, Any, Any] | AsyncGenerator[Any, Any], covariant=True)
3637
_P = ParamSpec("_P")
3738

39+
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None)
40+
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None)
41+
3842
_ExitFunc: TypeAlias = Callable[[type[BaseException] | None, BaseException | None, TracebackType | None], bool | None]
3943
_CM_EF = TypeVar("_CM_EF", bound=AbstractContextManager[Any, Any] | _ExitFunc)
4044

@@ -64,16 +68,19 @@ class ContextDecorator:
6468
def _recreate_cm(self) -> Self: ...
6569
def __call__(self, func: _F) -> _F: ...
6670

67-
class _GeneratorContextManagerBase: ...
68-
69-
class _GeneratorContextManager(_GeneratorContextManagerBase, AbstractContextManager[_T_co, bool | None], ContextDecorator):
70-
# __init__ and all instance attributes are actually inherited from _GeneratorContextManagerBase
71-
# adding them there is more trouble than it's worth to include in the stub; see #6676
72-
def __init__(self, func: Callable[..., Iterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
73-
gen: Generator[_T_co, Any, Any]
74-
func: Callable[..., Generator[_T_co, Any, Any]]
71+
class _GeneratorContextManagerBase(Generic[_G]):
72+
# Ideally this would use ParamSpec, but that requires (*args, **kwargs), which this isn't. see #6676
73+
def __init__(self, func: Callable[..., _G], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
74+
gen: _G
75+
func: Callable[..., _G]
7576
args: tuple[Any, ...]
7677
kwds: dict[str, Any]
78+
79+
class _GeneratorContextManager(
80+
_GeneratorContextManagerBase[Generator[_T_co, _SendT_contra, _ReturnT_co]],
81+
AbstractContextManager[_T_co, bool | None],
82+
ContextDecorator,
83+
):
7784
if sys.version_info >= (3, 9):
7885
def __exit__(
7986
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
@@ -93,26 +100,18 @@ if sys.version_info >= (3, 10):
93100
def __call__(self, func: _AF) -> _AF: ...
94101

95102
class _AsyncGeneratorContextManager(
96-
_GeneratorContextManagerBase, AbstractAsyncContextManager[_T_co, bool | None], AsyncContextDecorator
103+
_GeneratorContextManagerBase[AsyncGenerator[_T_co, _SendT_contra]],
104+
AbstractAsyncContextManager[_T_co, bool | None],
105+
AsyncContextDecorator,
97106
):
98-
# __init__ and these attributes are actually defined in the base class _GeneratorContextManagerBase,
99-
# adding them there is more trouble than it's worth to include in the stub (see #6676)
100-
def __init__(self, func: Callable[..., AsyncIterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
101-
gen: AsyncGenerator[_T_co, Any]
102-
func: Callable[..., AsyncGenerator[_T_co, Any]]
103-
args: tuple[Any, ...]
104-
kwds: dict[str, Any]
105107
async def __aexit__(
106108
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
107109
) -> bool | None: ...
108110

109111
else:
110-
class _AsyncGeneratorContextManager(_GeneratorContextManagerBase, AbstractAsyncContextManager[_T_co, bool | None]):
111-
def __init__(self, func: Callable[..., AsyncIterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
112-
gen: AsyncGenerator[_T_co, Any]
113-
func: Callable[..., AsyncGenerator[_T_co, Any]]
114-
args: tuple[Any, ...]
115-
kwds: dict[str, Any]
112+
class _AsyncGeneratorContextManager(
113+
_GeneratorContextManagerBase[AsyncGenerator[_T_co, _SendT_contra]], AbstractAsyncContextManager[_T_co, bool | None]
114+
):
116115
async def __aexit__(
117116
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
118117
) -> bool | None: ...

0 commit comments

Comments
 (0)