Skip to content

Commit bf795a1

Browse files
committed
add _GeneratorContextManagerBase.__init__
I think that #6676 showed that Paramspec didn't work, but that wasn't actually the fault of _GeneratorContextManagerBase.
1 parent 364fd7d commit bf795a1

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ asyncio.BaseEventLoop.subprocess_exec # BaseEventLoop adds several parameters a
1212
asyncio.base_events.BaseEventLoop.subprocess_exec # BaseEventLoop adds several parameters and stubtest fails on the difference if we add them
1313
builtins.dict.get
1414
collections\.ChainMap\.fromkeys # https://github.com/python/mypy/issues/17023
15-
contextlib._GeneratorContextManagerBase.__init__ # skipped in the stubs in favor of its child classes
1615
ctypes.CDLL._FuncPtr # None at class level but initialized in __init__ to this value
1716
ctypes.memmove # CFunctionType
1817
ctypes.memset # CFunctionType

stdlib/contextlib.pyi

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import abc
22
import sys
33
from _typeshed import FileDescriptorOrPath, Unused
44
from abc import ABC, abstractmethod
5-
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
5+
from collections.abc import AsyncIterator, Awaitable, Callable, Generator, Iterator
66
from types import TracebackType
77
from typing import IO, Any, Generic, Protocol, TypeVar, overload, runtime_checkable
88
from typing_extensions import ParamSpec, Self, TypeAlias
@@ -64,16 +64,15 @@ class ContextDecorator:
6464
def _recreate_cm(self) -> Self: ...
6565
def __call__(self, func: _F) -> _F: ...
6666

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
67+
class _GeneratorContextManagerBase(Generic[_T_co]):
68+
# Ideally this would use Paramspec, but that requires (*args, **kwargs), which this isn't. see #6676
7269
def __init__(self, func: Callable[..., Iterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
7370
gen: Generator[_T_co, Any, Any]
7471
func: Callable[..., Generator[_T_co, Any, Any]]
7572
args: tuple[Any, ...]
7673
kwds: dict[str, Any]
74+
75+
class _GeneratorContextManager(_GeneratorContextManagerBase[_T_co], AbstractContextManager[_T_co, bool | None], ContextDecorator):
7776
if sys.version_info >= (3, 9):
7877
def __exit__(
7978
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
@@ -93,26 +92,14 @@ if sys.version_info >= (3, 10):
9392
def __call__(self, func: _AF) -> _AF: ...
9493

9594
class _AsyncGeneratorContextManager(
96-
_GeneratorContextManagerBase, AbstractAsyncContextManager[_T_co, bool | None], AsyncContextDecorator
95+
_GeneratorContextManagerBase[_T_co], AbstractAsyncContextManager[_T_co, bool | None], AsyncContextDecorator
9796
):
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]
10597
async def __aexit__(
10698
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
10799
) -> bool | None: ...
108100

109101
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]
102+
class _AsyncGeneratorContextManager(_GeneratorContextManagerBase[_T_co], AbstractAsyncContextManager[_T_co, bool | None]):
116103
async def __aexit__(
117104
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
118105
) -> bool | None: ...

0 commit comments

Comments
 (0)