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

Skip to content

Commit 0e75580

Browse files
authored
Add missing attributes to contextlib._(Async)GeneratorContextManager (#6676)
1 parent 9efc0c0 commit 0e75580

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

stdlib/contextlib.pyi

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ from types import TracebackType
44
from typing import (
55
IO,
66
Any,
7+
AsyncGenerator,
78
AsyncIterator,
89
Awaitable,
910
Callable,
1011
ContextManager,
12+
Generator,
1113
Generic,
1214
Iterator,
1315
Optional,
@@ -36,22 +38,41 @@ _CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)
3638
class ContextDecorator:
3739
def __call__(self, func: _F) -> _F: ...
3840

39-
class _GeneratorContextManager(AbstractContextManager[_T_co], ContextDecorator): ...
41+
class _GeneratorContextManager(AbstractContextManager[_T_co], ContextDecorator, Generic[_T_co]):
42+
# In Python <= 3.6, __init__ and all instance attributes are defined directly on this class.
43+
# In Python >= 3.7, __init__ and all instance attributes are inherited from _GeneratorContextManagerBase
44+
# _GeneratorContextManagerBase is more trouble than it's worth to include in the stub; see #6676
45+
def __init__(self, func: Callable[..., Iterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
46+
gen: Generator[_T_co, Any, Any]
47+
func: Callable[..., Generator[_T_co, Any, Any]]
48+
args: tuple[Any, ...]
49+
kwds: dict[str, Any]
4050

41-
# type ignore to deal with incomplete ParamSpec support in mypy
42-
def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ... # type: ignore[misc]
51+
def contextmanager(func: Callable[_P, Iterator[_T_co]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
4352

4453
if sys.version_info >= (3, 10):
4554
_AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]])
4655
class AsyncContextDecorator:
4756
def __call__(self, func: _AF) -> _AF: ...
48-
class _AsyncGeneratorContextManager(AbstractAsyncContextManager[_T_co], AsyncContextDecorator): ...
57+
class _AsyncGeneratorContextManager(AbstractAsyncContextManager[_T_co], AsyncContextDecorator, Generic[_T_co]):
58+
# __init__ and these attributes are actually defined in the base class _GeneratorContextManagerBase,
59+
# which is more trouble than it's worth to include in the stub (see #6676)
60+
def __init__(self, func: Callable[..., AsyncIterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
61+
gen: AsyncGenerator[_T_co, Any]
62+
func: Callable[..., AsyncGenerator[_T_co, Any]]
63+
args: tuple[Any, ...]
64+
kwds: dict[str, Any]
4965

5066
elif sys.version_info >= (3, 7):
51-
class _AsyncGeneratorContextManager(AbstractAsyncContextManager[_T_co]): ...
67+
class _AsyncGeneratorContextManager(AbstractAsyncContextManager[_T_co], Generic[_T_co]):
68+
def __init__(self, func: Callable[..., AsyncIterator[_T_co]], args: tuple[Any, ...], kwds: dict[str, Any]) -> None: ...
69+
gen: AsyncGenerator[_T_co, Any]
70+
func: Callable[..., AsyncGenerator[_T_co, Any]]
71+
args: tuple[Any, ...]
72+
kwds: dict[str, Any]
5273

5374
if sys.version_info >= (3, 7):
54-
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, _AsyncGeneratorContextManager[_T]]: ... # type: ignore[misc]
75+
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
5576

5677
class _SupportsClose(Protocol):
5778
def close(self) -> object: ...

tests/stubtest_allowlists/py36.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ collections.AsyncGenerator.ag_frame
2020
collections.AsyncGenerator.ag_running
2121
collections.Callable
2222
collections.UserString.maketrans
23-
contextlib._GeneratorContextManager.__init__
2423
datetime.datetime_CAPI
2524
distutils.cygwinccompiler.RE_VERSION
2625
distutils.dist.command_re

0 commit comments

Comments
 (0)