From 3f41b30535ad614701a47e47659a5d7c89a6bbe8 Mon Sep 17 00:00:00 2001 From: Alyssa Coghlan Date: Fri, 14 Feb 2025 14:49:51 +1000 Subject: [PATCH] Revert "Improve [Async]ContextDecorator type hinting (#13416)" (#13436) This reverts commit 57d7c4334b64856fda6f6e8f992b101ddafe2f57. 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 --- stdlib/contextlib.pyi | 12 ++++-------- stubs/decorator/decorator.pyi | 4 +++- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/stdlib/contextlib.pyi b/stdlib/contextlib.pyi index e1d5f91fa..f57e7fa67 100644 --- a/stdlib/contextlib.pyi +++ b/stdlib/contextlib.pyi @@ -32,9 +32,9 @@ _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) _T_io = TypeVar("_T_io", bound=IO[str] | None) _ExitT_co = TypeVar("_ExitT_co", covariant=True, bound=bool | None, default=bool | None) +_F = TypeVar("_F", bound=Callable[..., Any]) _G = TypeVar("_G", bound=Generator[Any, Any, Any] | AsyncGenerator[Any, Any], covariant=True) _P = ParamSpec("_P") -_R = TypeVar("_R") _SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None) _ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None) @@ -64,13 +64,9 @@ class AbstractAsyncContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ign self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, / ) -> _ExitT_co: ... -class _WrappedCallable(Generic[_P, _R]): - __wrapped__: Callable[_P, _R] - def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ... - class ContextDecorator: def _recreate_cm(self) -> Self: ... - def __call__(self, func: Callable[_P, _R]) -> _WrappedCallable[_P, _R]: ... + def __call__(self, func: _F) -> _F: ... class _GeneratorContextManagerBase(Generic[_G]): # Ideally this would use ParamSpec, but that requires (*args, **kwargs), which this isn't. see #6676 @@ -97,11 +93,11 @@ class _GeneratorContextManager( def contextmanager(func: Callable[_P, Iterator[_T_co]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ... if sys.version_info >= (3, 10): - _AR = TypeVar("_AR", bound=Awaitable[Any]) + _AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]]) class AsyncContextDecorator: def _recreate_cm(self) -> Self: ... - def __call__(self, func: Callable[_P, _AR]) -> _WrappedCallable[_P, _AR]: ... + def __call__(self, func: _AF) -> _AF: ... class _AsyncGeneratorContextManager( _GeneratorContextManagerBase[AsyncGenerator[_T_co, _SendT_contra]], diff --git a/stubs/decorator/decorator.pyi b/stubs/decorator/decorator.pyi index 7ced22b9c..d7631417d 100644 --- a/stubs/decorator/decorator.pyi +++ b/stubs/decorator/decorator.pyi @@ -7,6 +7,7 @@ from re import Pattern from typing import Any, Literal, TypeVar from typing_extensions import ParamSpec +_C = TypeVar("_C", bound=Callable[..., Any]) _Func = TypeVar("_Func", bound=Callable[..., Any]) _T = TypeVar("_T") _P = ParamSpec("_P") @@ -64,7 +65,8 @@ def decorator( caller: Callable[..., Any], _func: Callable[..., Any] | None = ... ) -> Callable[[Callable[..., Any]], Callable[..., Any]]: ... -class ContextManager(_GeneratorContextManager[_T]): ... +class ContextManager(_GeneratorContextManager[_T]): + def __call__(self, func: _C) -> _C: ... def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, ContextManager[_T]]: ... def append(a: type, vancestors: list[type]) -> None: ...