Skip to content

Commit ea61ca5

Browse files
authored
[3.13] typing.(Async)ContextManager adds second type parameter (#11873)
This is in response to: python/cpython#118681
1 parent 120f01d commit ea61ca5

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

stdlib/typing.pyi

+12-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import typing_extensions
88
from _collections_abc import dict_items, dict_keys, dict_values
99
from _typeshed import IdentityFunction, ReadableBuffer, SupportsKeysAndGetItem
1010
from abc import ABCMeta, abstractmethod
11-
from contextlib import AbstractAsyncContextManager, AbstractContextManager
1211
from re import Match as Match, Pattern as Pattern
1312
from types import (
1413
BuiltinFunctionType,
@@ -24,10 +23,10 @@ from types import (
2423
)
2524
from typing_extensions import Never as _Never, ParamSpec as _ParamSpec
2625

27-
if sys.version_info >= (3, 10):
28-
from types import UnionType
2926
if sys.version_info >= (3, 9):
3027
from types import GenericAlias
28+
if sys.version_info >= (3, 10):
29+
from types import UnionType
3130

3231
__all__ = [
3332
"AbstractSet",
@@ -428,17 +427,17 @@ class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _Return
428427
@property
429428
def gi_yieldfrom(self) -> Generator[Any, Any, Any] | None: ...
430429

431-
# NOTE: Technically we would like this to be able to accept a second parameter as well, just
432-
# like it's counterpart in contextlib, however `typing._SpecialGenericAlias` enforces the
433-
# correct number of arguments at runtime, so we would be hiding runtime errors.
434-
@runtime_checkable
435-
class ContextManager(AbstractContextManager[_T_co, bool | None], Protocol[_T_co]): ...
430+
# NOTE: Prior to Python 3.13 these aliases are lacking the second _ExitT_co parameter
431+
if sys.version_info >= (3, 13):
432+
from contextlib import AbstractAsyncContextManager as AsyncContextManager, AbstractContextManager as ContextManager
433+
else:
434+
from contextlib import AbstractAsyncContextManager, AbstractContextManager
436435

437-
# NOTE: Technically we would like this to be able to accept a second parameter as well, just
438-
# like it's counterpart in contextlib, however `typing._SpecialGenericAlias` enforces the
439-
# correct number of arguments at runtime, so we would be hiding runtime errors.
440-
@runtime_checkable
441-
class AsyncContextManager(AbstractAsyncContextManager[_T_co, bool | None], Protocol[_T_co]): ...
436+
@runtime_checkable
437+
class ContextManager(AbstractContextManager[_T_co, bool | None], Protocol[_T_co]): ...
438+
439+
@runtime_checkable
440+
class AsyncContextManager(AbstractAsyncContextManager[_T_co, bool | None], Protocol[_T_co]): ...
442441

443442
@runtime_checkable
444443
class Awaitable(Protocol[_T_co]):

0 commit comments

Comments
 (0)