Skip to content

Commit 57d7c43

Browse files
authored
Improve [Async]ContextDecorator type hinting (#13416)
Updated annotations allow access to `__wrapped__` on decorated callables without complaints from typecheckers.
1 parent c951dbb commit 57d7c43

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

stdlib/contextlib.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ _T = TypeVar("_T")
3232
_T_co = TypeVar("_T_co", covariant=True)
3333
_T_io = TypeVar("_T_io", bound=IO[str] | None)
3434
_ExitT_co = TypeVar("_ExitT_co", covariant=True, bound=bool | None, default=bool | None)
35-
_F = TypeVar("_F", bound=Callable[..., Any])
3635
_G = TypeVar("_G", bound=Generator[Any, Any, Any] | AsyncGenerator[Any, Any], covariant=True)
3736
_P = ParamSpec("_P")
37+
_R = TypeVar("_R")
3838

3939
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None)
4040
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None)
@@ -64,9 +64,13 @@ class AbstractAsyncContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ign
6464
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, /
6565
) -> _ExitT_co: ...
6666

67+
class _WrappedCallable(Generic[_P, _R]):
68+
__wrapped__: Callable[_P, _R]
69+
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
70+
6771
class ContextDecorator:
6872
def _recreate_cm(self) -> Self: ...
69-
def __call__(self, func: _F) -> _F: ...
73+
def __call__(self, func: Callable[_P, _R]) -> _WrappedCallable[_P, _R]: ...
7074

7175
class _GeneratorContextManagerBase(Generic[_G]):
7276
# Ideally this would use ParamSpec, but that requires (*args, **kwargs), which this isn't. see #6676
@@ -93,11 +97,11 @@ class _GeneratorContextManager(
9397
def contextmanager(func: Callable[_P, Iterator[_T_co]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
9498

9599
if sys.version_info >= (3, 10):
96-
_AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]])
100+
_AR = TypeVar("_AR", bound=Awaitable[Any])
97101

98102
class AsyncContextDecorator:
99103
def _recreate_cm(self) -> Self: ...
100-
def __call__(self, func: _AF) -> _AF: ...
104+
def __call__(self, func: Callable[_P, _AR]) -> _WrappedCallable[_P, _AR]: ...
101105

102106
class _AsyncGeneratorContextManager(
103107
_GeneratorContextManagerBase[AsyncGenerator[_T_co, _SendT_contra]],

stubs/decorator/decorator.pyi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ from re import Pattern
77
from typing import Any, Literal, TypeVar
88
from typing_extensions import ParamSpec
99

10-
_C = TypeVar("_C", bound=Callable[..., Any])
1110
_Func = TypeVar("_Func", bound=Callable[..., Any])
1211
_T = TypeVar("_T")
1312
_P = ParamSpec("_P")
@@ -65,8 +64,7 @@ def decorator(
6564
caller: Callable[..., Any], _func: Callable[..., Any] | None = ...
6665
) -> Callable[[Callable[..., Any]], Callable[..., Any]]: ...
6766

68-
class ContextManager(_GeneratorContextManager[_T]):
69-
def __call__(self, func: _C) -> _C: ...
67+
class ContextManager(_GeneratorContextManager[_T]): ...
7068

7169
def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, ContextManager[_T]]: ...
7270
def append(a: type, vancestors: list[type]) -> None: ...

0 commit comments

Comments
 (0)