@@ -8,6 +8,7 @@ from typing import (
8
8
Awaitable ,
9
9
Callable ,
10
10
ContextManager ,
11
+ Generator ,
11
12
Generic ,
12
13
Iterator ,
13
14
Optional ,
@@ -26,6 +27,8 @@ if sys.version_info >= (3, 7):
26
27
27
28
_T = TypeVar ("_T" )
28
29
_T_co = TypeVar ("_T_co" , covariant = True )
30
+ _T_contra = TypeVar ("_T_contra" , contravariant = True )
31
+ _V_co = TypeVar ("_V_co" , covariant = True )
29
32
_T_io = TypeVar ("_T_io" , bound = Optional [IO [str ]])
30
33
_F = TypeVar ("_F" , bound = Callable [..., Any ])
31
34
_P = ParamSpec ("_P" )
@@ -36,7 +39,22 @@ _CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)
36
39
class ContextDecorator :
37
40
def __call__ (self , func : _F ) -> _F : ...
38
41
39
- class _GeneratorContextManager (AbstractContextManager [_T_co ], ContextDecorator ): ...
42
+ if sys .version_info >= (3 , 7 ):
43
+ class _GeneratorContextManagerBase (Generic [_P , _T_co , _T_contra , _V_co ]): # type: ignore[misc]
44
+ def __init__ (self , func : Callable [_P , Generator [_T_co , _T_contra , _V_co ]], args : _P .args , ** kwds : _P .kwargs ) -> None : ...
45
+ gen : Generator [_T_co , _T_contra , _V_co ]
46
+ func : Callable [_P , Generator [_T_co , _T_contra , _V_co ]] # type: ignore[misc]
47
+ args : _P .args
48
+ kwds : _P .kwargs
49
+ class _GeneratorContextManager ( # type: ignore[misc]
50
+ _GeneratorContextManagerBase [_P , _T_co , _T_contra , _V_co ], # type: ignore[misc]
51
+ AbstractContextManager [_T_co ],
52
+ ContextDecorator ,
53
+ Generic [_P , _T_co , _T_contra , _V_co ], # type: ignore[misc]
54
+ ): ...
55
+
56
+ else :
57
+ class _GeneratorContextManager (AbstractContextManager [_T_co ], ContextDecorator ): ...
40
58
41
59
# type ignore to deal with incomplete ParamSpec support in mypy
42
60
def contextmanager (func : Callable [_P , Iterator [_T ]]) -> Callable [_P , _GeneratorContextManager [_T ]]: ... # type: ignore[misc]
@@ -45,7 +63,12 @@ if sys.version_info >= (3, 10):
45
63
_AF = TypeVar ("_AF" , bound = Callable [..., Awaitable [Any ]])
46
64
class AsyncContextDecorator :
47
65
def __call__ (self , func : _AF ) -> _AF : ...
48
- class _AsyncGeneratorContextManager (AbstractAsyncContextManager [_T_co ], AsyncContextDecorator ): ...
66
+ class _AsyncGeneratorContextManager (
67
+ _GeneratorContextManagerBase [_P , _T_co , _T_contra , _V_co ],
68
+ AbstractAsyncContextManager [_T_co ],
69
+ AsyncContextDecorator ,
70
+ Generic [_P , _T_co , _T_contra , _V_co ],
71
+ ): ...
49
72
50
73
elif sys .version_info >= (3 , 7 ):
51
74
class _AsyncGeneratorContextManager (AbstractAsyncContextManager [_T_co ]): ...
0 commit comments