@@ -4,10 +4,12 @@ from types import TracebackType
4
4
from typing import (
5
5
IO ,
6
6
Any ,
7
+ AsyncGenerator ,
7
8
AsyncIterator ,
8
9
Awaitable ,
9
10
Callable ,
10
11
ContextManager ,
12
+ Generator ,
11
13
Generic ,
12
14
Iterator ,
13
15
Optional ,
@@ -36,22 +38,41 @@ _CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)
36
38
class ContextDecorator :
37
39
def __call__ (self , func : _F ) -> _F : ...
38
40
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 ]
40
50
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 ]]: ...
43
52
44
53
if sys .version_info >= (3 , 10 ):
45
54
_AF = TypeVar ("_AF" , bound = Callable [..., Awaitable [Any ]])
46
55
class AsyncContextDecorator :
47
56
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 ]
49
65
50
66
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 ]
52
73
53
74
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 ]]: ...
55
76
56
77
class _SupportsClose (Protocol ):
57
78
def close (self ) -> object : ...
0 commit comments