@@ -33,8 +33,12 @@ _T_co = TypeVar("_T_co", covariant=True)
33
33
_T_io = TypeVar ("_T_io" , bound = IO [str ] | None )
34
34
_ExitT_co = TypeVar ("_ExitT_co" , covariant = True , bound = bool | None , default = bool | None )
35
35
_F = TypeVar ("_F" , bound = Callable [..., Any ])
36
+ _G = TypeVar ("_G" , bound = Generator [Any , Any , Any ] | AsyncGenerator [Any , Any ], covariant = True )
36
37
_P = ParamSpec ("_P" )
37
38
39
+ _SendT_contra = TypeVar ("_SendT_contra" , contravariant = True , default = None )
40
+ _ReturnT_co = TypeVar ("_ReturnT_co" , covariant = True , default = None )
41
+
38
42
_ExitFunc : TypeAlias = Callable [[type [BaseException ] | None , BaseException | None , TracebackType | None ], bool | None ]
39
43
_CM_EF = TypeVar ("_CM_EF" , bound = AbstractContextManager [Any , Any ] | _ExitFunc )
40
44
@@ -64,16 +68,19 @@ class ContextDecorator:
64
68
def _recreate_cm (self ) -> Self : ...
65
69
def __call__ (self , func : _F ) -> _F : ...
66
70
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 ]
75
76
args : tuple [Any , ...]
76
77
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
+ ):
77
84
if sys .version_info >= (3 , 9 ):
78
85
def __exit__ (
79
86
self , typ : type [BaseException ] | None , value : BaseException | None , traceback : TracebackType | None
@@ -93,26 +100,18 @@ if sys.version_info >= (3, 10):
93
100
def __call__ (self , func : _AF ) -> _AF : ...
94
101
95
102
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 ,
97
106
):
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 ]
105
107
async def __aexit__ (
106
108
self , typ : type [BaseException ] | None , value : BaseException | None , traceback : TracebackType | None
107
109
) -> bool | None : ...
108
110
109
111
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
+ ):
116
115
async def __aexit__ (
117
116
self , typ : type [BaseException ] | None , value : BaseException | None , traceback : TracebackType | None
118
117
) -> bool | None : ...
0 commit comments