Make ExitStack, AbstractContextManager and AsyncAbstractContextManager generic in return type of __exit__ (#11048)

This commit is contained in:
David Salvisberg
2024-04-22 16:43:08 +02:00
committed by GitHub
parent b3bfdadb45
commit f274c78fe5
5 changed files with 42 additions and 28 deletions

View File

@@ -129,9 +129,6 @@ if sys.version_info >= (3, 11):
if sys.version_info >= (3, 12):
__all__ += ["TypeAliasType", "override"]
ContextManager = AbstractContextManager
AsyncContextManager = AbstractAsyncContextManager
Any = object()
def final(f: _T) -> _T: ...
@@ -431,6 +428,18 @@ class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _Return
@property
def gi_yieldfrom(self) -> Generator[Any, Any, Any] | None: ...
# NOTE: Technically we would like this to be able to accept a second parameter as well, just
# like it's counterpart in contextlib, however `typing._SpecialGenericAlias` enforces the
# correct number of arguments at runtime, so we would be hiding runtime errors.
@runtime_checkable
class ContextManager(AbstractContextManager[_T_co, bool | None], Protocol[_T_co]): ...
# NOTE: Technically we would like this to be able to accept a second parameter as well, just
# like it's counterpart in contextlib, however `typing._SpecialGenericAlias` enforces the
# correct number of arguments at runtime, so we would be hiding runtime errors.
@runtime_checkable
class AsyncContextManager(AbstractAsyncContextManager[_T_co, bool | None], Protocol[_T_co]): ...
@runtime_checkable
class Awaitable(Protocol[_T_co]):
@abstractmethod