Skip to content

Commit 3f41b30

Browse files
authored
Revert "Improve [Async]ContextDecorator type hinting (#13416)" (#13436)
This reverts commit 57d7c43. The attempted fix loses all type overload information during type inferencing, so postpone fixing the issue until we have a solution which doesn't impose such a dramatic loss in functionality. Reopens #13403
1 parent a410f25 commit 3f41b30

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

Diff for: stdlib/contextlib.pyi

+4-8
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])
3536
_G = TypeVar("_G", bound=Generator[Any, Any, Any] | AsyncGenerator[Any, Any], covariant=True)
3637
_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,13 +64,9 @@ 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-
7167
class ContextDecorator:
7268
def _recreate_cm(self) -> Self: ...
73-
def __call__(self, func: Callable[_P, _R]) -> _WrappedCallable[_P, _R]: ...
69+
def __call__(self, func: _F) -> _F: ...
7470

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

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

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

106102
class _AsyncGeneratorContextManager(
107103
_GeneratorContextManagerBase[AsyncGenerator[_T_co, _SendT_contra]],

Diff for: stubs/decorator/decorator.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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])
1011
_Func = TypeVar("_Func", bound=Callable[..., Any])
1112
_T = TypeVar("_T")
1213
_P = ParamSpec("_P")
@@ -64,7 +65,8 @@ def decorator(
6465
caller: Callable[..., Any], _func: Callable[..., Any] | None = ...
6566
) -> Callable[[Callable[..., Any]], Callable[..., Any]]: ...
6667

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

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

0 commit comments

Comments
 (0)