From 3d822e96e3aa837e190815ae377088e76291d0f3 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Feb 2025 10:26:10 +0300 Subject: [PATCH 1/6] Refactor `@reawaitable` decorator --- returns/primitives/reawaitable.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/returns/primitives/reawaitable.py b/returns/primitives/reawaitable.py index fba8505d3..d3327083f 100644 --- a/returns/primitives/reawaitable.py +++ b/returns/primitives/reawaitable.py @@ -1,8 +1,9 @@ from collections.abc import Awaitable, Callable, Generator -from typing import NewType, TypeVar, cast, final +from functools import wraps +from typing import NewType, TypeVar, ParamSpec, cast, final _ValueType = TypeVar('_ValueType') -_FunctionCoroType = TypeVar('_FunctionCoroType', bound=Callable[..., Awaitable]) +_Params = ParamSpec('_Params') _Sentinel = NewType('_Sentinel', object) _sentinel: _Sentinel = cast(_Sentinel, object()) @@ -104,7 +105,9 @@ async def _awaitable(self) -> _ValueType: return self._cache # type: ignore -def reawaitable(coro: _FunctionCoroType) -> _FunctionCoroType: +def reawaitable( + coro: Callable[_Params, Awaitable[_ValueType]], +) -> Callable[_Params, Awaitable[_ValueType]]: """ Allows to decorate coroutine functions to be awaitable multiple times. @@ -124,6 +127,7 @@ def reawaitable(coro: _FunctionCoroType) -> _FunctionCoroType: >>> assert anyio.run(main) == 3 """ - return lambda *args, **kwargs: ReAwaitable( # type: ignore - coro(*args, **kwargs), - ) + @wraps(coro) + def decorator(*args: _Params.args, **kwargs: _Params.kwargs) -> Awaitable[_ValueType]: + return ReAwaitable(coro(*args, **kwargs)) + return decorator From d41c9df851642a3ffa2d40c173538ab30a47f173 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 1 Feb 2025 07:26:53 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- returns/primitives/reawaitable.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/returns/primitives/reawaitable.py b/returns/primitives/reawaitable.py index d3327083f..fbce909ec 100644 --- a/returns/primitives/reawaitable.py +++ b/returns/primitives/reawaitable.py @@ -1,6 +1,6 @@ from collections.abc import Awaitable, Callable, Generator from functools import wraps -from typing import NewType, TypeVar, ParamSpec, cast, final +from typing import NewType, ParamSpec, TypeVar, cast, final _ValueType = TypeVar('_ValueType') _Params = ParamSpec('_Params') @@ -127,7 +127,11 @@ def reawaitable( >>> assert anyio.run(main) == 3 """ + @wraps(coro) - def decorator(*args: _Params.args, **kwargs: _Params.kwargs) -> Awaitable[_ValueType]: + def decorator( + *args: _Params.args, **kwargs: _Params.kwargs + ) -> Awaitable[_ValueType]: return ReAwaitable(coro(*args, **kwargs)) + return decorator From d8dba6b85f97b1d95c364b1d5870ba6c550c4012 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Feb 2025 10:30:16 +0300 Subject: [PATCH 3/6] Update reawaitable.py --- returns/primitives/reawaitable.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/returns/primitives/reawaitable.py b/returns/primitives/reawaitable.py index fbce909ec..1c393a53b 100644 --- a/returns/primitives/reawaitable.py +++ b/returns/primitives/reawaitable.py @@ -3,7 +3,7 @@ from typing import NewType, ParamSpec, TypeVar, cast, final _ValueType = TypeVar('_ValueType') -_Params = ParamSpec('_Params') +_PS = ParamSpec('_PS') _Sentinel = NewType('_Sentinel', object) _sentinel: _Sentinel = cast(_Sentinel, object()) @@ -106,8 +106,8 @@ async def _awaitable(self) -> _ValueType: def reawaitable( - coro: Callable[_Params, Awaitable[_ValueType]], -) -> Callable[_Params, Awaitable[_ValueType]]: + coro: Callable[_PS, Awaitable[_ValueType]], +) -> Callable[_PS, Awaitable[_ValueType]]: """ Allows to decorate coroutine functions to be awaitable multiple times. @@ -130,7 +130,7 @@ def reawaitable( @wraps(coro) def decorator( - *args: _Params.args, **kwargs: _Params.kwargs + *args: _PS.args, **kwargs: _PS.kwargs ) -> Awaitable[_ValueType]: return ReAwaitable(coro(*args, **kwargs)) From 251a6328a415dea015d8f11233cc43ad20f07889 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Feb 2025 10:30:50 +0300 Subject: [PATCH 4/6] Update reawaitable.py --- returns/primitives/reawaitable.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/returns/primitives/reawaitable.py b/returns/primitives/reawaitable.py index 1c393a53b..38645785a 100644 --- a/returns/primitives/reawaitable.py +++ b/returns/primitives/reawaitable.py @@ -3,7 +3,7 @@ from typing import NewType, ParamSpec, TypeVar, cast, final _ValueType = TypeVar('_ValueType') -_PS = ParamSpec('_PS') +_Ps = ParamSpec('_Ps') _Sentinel = NewType('_Sentinel', object) _sentinel: _Sentinel = cast(_Sentinel, object()) @@ -106,8 +106,8 @@ async def _awaitable(self) -> _ValueType: def reawaitable( - coro: Callable[_PS, Awaitable[_ValueType]], -) -> Callable[_PS, Awaitable[_ValueType]]: + coro: Callable[_Ps, Awaitable[_ValueType]], +) -> Callable[_Ps, Awaitable[_ValueType]]: """ Allows to decorate coroutine functions to be awaitable multiple times. @@ -130,7 +130,7 @@ def reawaitable( @wraps(coro) def decorator( - *args: _PS.args, **kwargs: _PS.kwargs + *args: _Ps.args, **kwargs: _Ps.kwargs ) -> Awaitable[_ValueType]: return ReAwaitable(coro(*args, **kwargs)) From 0d608714eff735b25f1fad180ec341a8d3d10c7b Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Feb 2025 10:35:06 +0300 Subject: [PATCH 5/6] Update reawaitable.py --- returns/primitives/reawaitable.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/returns/primitives/reawaitable.py b/returns/primitives/reawaitable.py index 38645785a..3e42a54bc 100644 --- a/returns/primitives/reawaitable.py +++ b/returns/primitives/reawaitable.py @@ -3,6 +3,7 @@ from typing import NewType, ParamSpec, TypeVar, cast, final _ValueType = TypeVar('_ValueType') +_AwaitableT = TypeVar('_AwaitableT', bound=Awaitable) _Ps = ParamSpec('_Ps') _Sentinel = NewType('_Sentinel', object) @@ -106,8 +107,8 @@ async def _awaitable(self) -> _ValueType: def reawaitable( - coro: Callable[_Ps, Awaitable[_ValueType]], -) -> Callable[_Ps, Awaitable[_ValueType]]: + coro: Callable[_Ps, _AwaitableT], +) -> Callable[_Ps, _AwaitableT]: """ Allows to decorate coroutine functions to be awaitable multiple times. @@ -130,8 +131,9 @@ def reawaitable( @wraps(coro) def decorator( - *args: _Ps.args, **kwargs: _Ps.kwargs - ) -> Awaitable[_ValueType]: - return ReAwaitable(coro(*args, **kwargs)) + *args: _Ps.args, + **kwargs: _Ps.kwargs, + ) -> _AwaitableT: + return ReAwaitable(coro(*args, **kwargs)) # type: ignore[return-type] return decorator From fa97ae6dbf0862f35029e4341a7abefeade369bf Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 1 Feb 2025 10:38:48 +0300 Subject: [PATCH 6/6] Update reawaitable.py --- returns/primitives/reawaitable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/returns/primitives/reawaitable.py b/returns/primitives/reawaitable.py index 3e42a54bc..4e87d4717 100644 --- a/returns/primitives/reawaitable.py +++ b/returns/primitives/reawaitable.py @@ -134,6 +134,6 @@ def decorator( *args: _Ps.args, **kwargs: _Ps.kwargs, ) -> _AwaitableT: - return ReAwaitable(coro(*args, **kwargs)) # type: ignore[return-type] + return ReAwaitable(coro(*args, **kwargs)) # type: ignore[return-value] return decorator