@@ -31,32 +31,33 @@ if sys.version_info >= (3, 11):
3131_T = TypeVar ("_T" )
3232_T_co = TypeVar ("_T_co" , covariant = True )
3333_T_io = TypeVar ("_T_io" , bound = IO [str ] | None )
34+ _ExitT_co = TypeVar ("_ExitT_co" , covariant = True , bound = bool | None , default = bool | None )
3435_F = TypeVar ("_F" , bound = Callable [..., Any ])
3536_P = ParamSpec ("_P" )
3637
3738_ExitFunc : TypeAlias = Callable [[type [BaseException ] | None , BaseException | None , TracebackType | None ], bool | None ]
38- _CM_EF = TypeVar ("_CM_EF" , bound = AbstractContextManager [Any ] | _ExitFunc )
39+ _CM_EF = TypeVar ("_CM_EF" , bound = AbstractContextManager [Any , Any ] | _ExitFunc )
3940
4041@runtime_checkable
41- class AbstractContextManager (Protocol [_T_co ]):
42+ class AbstractContextManager (Protocol [_T_co , _ExitT_co ]):
4243 def __enter__ (self ) -> _T_co : ...
4344 @abstractmethod
4445 def __exit__ (
4546 self , exc_type : type [BaseException ] | None , exc_value : BaseException | None , traceback : TracebackType | None , /
46- ) -> bool | None : ...
47+ ) -> _ExitT_co : ...
4748
4849@runtime_checkable
49- class AbstractAsyncContextManager (Protocol [_T_co ]):
50+ class AbstractAsyncContextManager (Protocol [_T_co , _ExitT_co ]):
5051 async def __aenter__ (self ) -> _T_co : ...
5152 @abstractmethod
5253 async def __aexit__ (
5354 self , exc_type : type [BaseException ] | None , exc_value : BaseException | None , traceback : TracebackType | None , /
54- ) -> bool | None : ...
55+ ) -> _ExitT_co : ...
5556
5657class ContextDecorator :
5758 def __call__ (self , func : _F ) -> _F : ...
5859
59- class _GeneratorContextManager (AbstractContextManager [_T_co ], ContextDecorator ):
60+ class _GeneratorContextManager (AbstractContextManager [_T_co , bool | None ], ContextDecorator ):
6061 # __init__ and all instance attributes are actually inherited from _GeneratorContextManagerBase
6162 # _GeneratorContextManagerBase is more trouble than it's worth to include in the stub; see #6676
6263 def __init__ (self , func : Callable [..., Iterator [_T_co ]], args : tuple [Any , ...], kwds : dict [str , Any ]) -> None : ...
@@ -81,7 +82,7 @@ if sys.version_info >= (3, 10):
8182 class AsyncContextDecorator :
8283 def __call__ (self , func : _AF ) -> _AF : ...
8384
84- class _AsyncGeneratorContextManager (AbstractAsyncContextManager [_T_co ], AsyncContextDecorator ):
85+ class _AsyncGeneratorContextManager (AbstractAsyncContextManager [_T_co , bool | None ], AsyncContextDecorator ):
8586 # __init__ and these attributes are actually defined in the base class _GeneratorContextManagerBase,
8687 # which is more trouble than it's worth to include in the stub (see #6676)
8788 def __init__ (self , func : Callable [..., AsyncIterator [_T_co ]], args : tuple [Any , ...], kwds : dict [str , Any ]) -> None : ...
@@ -94,7 +95,7 @@ if sys.version_info >= (3, 10):
9495 ) -> bool | None : ...
9596
9697else :
97- class _AsyncGeneratorContextManager (AbstractAsyncContextManager [_T_co ]):
98+ class _AsyncGeneratorContextManager (AbstractAsyncContextManager [_T_co , bool | None ]):
9899 def __init__ (self , func : Callable [..., AsyncIterator [_T_co ]], args : tuple [Any , ...], kwds : dict [str , Any ]) -> None : ...
99100 gen : AsyncGenerator [_T_co , Any ]
100101 func : Callable [..., AsyncGenerator [_T_co , Any ]]
@@ -111,7 +112,7 @@ class _SupportsClose(Protocol):
111112
112113_SupportsCloseT = TypeVar ("_SupportsCloseT" , bound = _SupportsClose )
113114
114- class closing (AbstractContextManager [_SupportsCloseT ]):
115+ class closing (AbstractContextManager [_SupportsCloseT , None ]):
115116 def __init__ (self , thing : _SupportsCloseT ) -> None : ...
116117 def __exit__ (self , * exc_info : Unused ) -> None : ...
117118
@@ -121,17 +122,17 @@ if sys.version_info >= (3, 10):
121122
122123 _SupportsAcloseT = TypeVar ("_SupportsAcloseT" , bound = _SupportsAclose )
123124
124- class aclosing (AbstractAsyncContextManager [_SupportsAcloseT ]):
125+ class aclosing (AbstractAsyncContextManager [_SupportsAcloseT , None ]):
125126 def __init__ (self , thing : _SupportsAcloseT ) -> None : ...
126127 async def __aexit__ (self , * exc_info : Unused ) -> None : ...
127128
128- class suppress (AbstractContextManager [None ]):
129+ class suppress (AbstractContextManager [None , bool ]):
129130 def __init__ (self , * exceptions : type [BaseException ]) -> None : ...
130131 def __exit__ (
131132 self , exctype : type [BaseException ] | None , excinst : BaseException | None , exctb : TracebackType | None
132133 ) -> bool : ...
133134
134- class _RedirectStream (AbstractContextManager [_T_io ]):
135+ class _RedirectStream (AbstractContextManager [_T_io , None ]):
135136 def __init__ (self , new_target : _T_io ) -> None : ...
136137 def __exit__ (
137138 self , exctype : type [BaseException ] | None , excinst : BaseException | None , exctb : TracebackType | None
@@ -142,27 +143,27 @@ class redirect_stderr(_RedirectStream[_T_io]): ...
142143
143144# In reality this is a subclass of `AbstractContextManager`;
144145# see #7961 for why we don't do that in the stub
145- class ExitStack (metaclass = abc .ABCMeta ):
146- def enter_context (self , cm : AbstractContextManager [_T ]) -> _T : ...
146+ class ExitStack (Generic [ _ExitT_co ], metaclass = abc .ABCMeta ):
147+ def enter_context (self , cm : AbstractContextManager [_T , _ExitT_co ]) -> _T : ...
147148 def push (self , exit : _CM_EF ) -> _CM_EF : ...
148149 def callback (self , callback : Callable [_P , _T ], / , * args : _P .args , ** kwds : _P .kwargs ) -> Callable [_P , _T ]: ...
149150 def pop_all (self ) -> Self : ...
150151 def close (self ) -> None : ...
151152 def __enter__ (self ) -> Self : ...
152153 def __exit__ (
153154 self , exc_type : type [BaseException ] | None , exc_value : BaseException | None , traceback : TracebackType | None , /
154- ) -> bool : ...
155+ ) -> _ExitT_co : ...
155156
156157_ExitCoroFunc : TypeAlias = Callable [
157158 [type [BaseException ] | None , BaseException | None , TracebackType | None ], Awaitable [bool | None ]
158159]
159- _ACM_EF = TypeVar ("_ACM_EF" , bound = AbstractAsyncContextManager [Any ] | _ExitCoroFunc )
160+ _ACM_EF = TypeVar ("_ACM_EF" , bound = AbstractAsyncContextManager [Any , Any ] | _ExitCoroFunc )
160161
161162# In reality this is a subclass of `AbstractAsyncContextManager`;
162163# see #7961 for why we don't do that in the stub
163- class AsyncExitStack (metaclass = abc .ABCMeta ):
164- def enter_context (self , cm : AbstractContextManager [_T ]) -> _T : ...
165- async def enter_async_context (self , cm : AbstractAsyncContextManager [_T ]) -> _T : ...
164+ class AsyncExitStack (Generic [ _ExitT_co ], metaclass = abc .ABCMeta ):
165+ def enter_context (self , cm : AbstractContextManager [_T , _ExitT_co ]) -> _T : ...
166+ async def enter_async_context (self , cm : AbstractAsyncContextManager [_T , _ExitT_co ]) -> _T : ...
166167 def push (self , exit : _CM_EF ) -> _CM_EF : ...
167168 def push_async_exit (self , exit : _ACM_EF ) -> _ACM_EF : ...
168169 def callback (self , callback : Callable [_P , _T ], / , * args : _P .args , ** kwds : _P .kwargs ) -> Callable [_P , _T ]: ...
@@ -177,7 +178,7 @@ class AsyncExitStack(metaclass=abc.ABCMeta):
177178 ) -> bool : ...
178179
179180if sys .version_info >= (3 , 10 ):
180- class nullcontext (AbstractContextManager [_T ], AbstractAsyncContextManager [_T ]):
181+ class nullcontext (AbstractContextManager [_T , None ], AbstractAsyncContextManager [_T , None ]):
181182 enter_result : _T
182183 @overload
183184 def __init__ (self : nullcontext [None ], enter_result : None = None ) -> None : ...
@@ -189,7 +190,7 @@ if sys.version_info >= (3, 10):
189190 async def __aexit__ (self , * exctype : Unused ) -> None : ...
190191
191192else :
192- class nullcontext (AbstractContextManager [_T ]):
193+ class nullcontext (AbstractContextManager [_T , None ]):
193194 enter_result : _T
194195 @overload
195196 def __init__ (self : nullcontext [None ], enter_result : None = None ) -> None : ...
@@ -201,7 +202,7 @@ else:
201202if sys .version_info >= (3 , 11 ):
202203 _T_fd_or_any_path = TypeVar ("_T_fd_or_any_path" , bound = FileDescriptorOrPath )
203204
204- class chdir (AbstractContextManager [None ], Generic [_T_fd_or_any_path ]):
205+ class chdir (AbstractContextManager [None , None ], Generic [_T_fd_or_any_path ]):
205206 path : _T_fd_or_any_path
206207 def __init__ (self , path : _T_fd_or_any_path ) -> None : ...
207208 def __enter__ (self ) -> None : ...
0 commit comments