Skip to content

Commit 1a29e55

Browse files
authored
Refactor decorators (#1891)
1 parent a82b7f5 commit 1a29e55

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ See [0Ver](https://0ver.org/).
88

99
## 0.24.0 WIP
1010

11+
### Deprecated
12+
13+
- `returns.result.ResultE` alias
14+
- `returns.io.IOResultE` alias
15+
- `returns.future.FutureResultE` alias
16+
1117
### Features
1218

1319
- Add picky exceptions to `future_safe` decorator like `safe` has.
1420
- Improve inference of `ResultLike` objects when exception catching
1521
decorator is applied with explicit exception types
1622
- Add picky exceptions to `impure_safe` decorator like `safe` has. Issue #1543
1723

24+
### Misc
1825

19-
## Deprecated
20-
21-
- FutureResultE from future
26+
- Now requires `mypy>=1.11`
2227

2328

2429
## 0.23.0

returns/future.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
overload,
1616
)
1717

18-
from typing_extensions import ParamSpec
18+
from typing_extensions import ParamSpec, TypeAlias
1919

2020
from returns._internal.futures import _future, _future_result
2121
from returns.interfaces.specific.future import FutureBased1
@@ -1454,8 +1454,8 @@ def FutureFailure( # noqa: N802
14541454
return FutureResult.from_failure(inner_value)
14551455

14561456

1457-
# Deprecated
1458-
FutureResultE = FutureResult[_ValueType, Exception]
1457+
#: Deprecated alias for ``FutureResult[_ValueType, Exception]``.
1458+
FutureResultE: TypeAlias = FutureResult[_ValueType, Exception]
14591459

14601460

14611461
_ExceptionType = TypeVar('_ExceptionType', bound=Exception)

returns/io.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
overload,
1818
)
1919

20-
from typing_extensions import ParamSpec
20+
from typing_extensions import ParamSpec, TypeAlias
2121

2222
from returns.interfaces.specific import io, ioresult
2323
from returns.primitives.container import BaseContainer, container_equality
@@ -882,8 +882,8 @@ def lash(self, function):
882882
# Aliases:
883883

884884

885-
#: Alias for a popular case when ``IOResult`` has ``Exception`` as error type.
886-
IOResultE = IOResult[_ValueType, Exception]
885+
#: Deprecated alias for ``IOResult[_ValueType, Exception]``.
886+
IOResultE: TypeAlias = IOResult[_ValueType, Exception]
887887

888888

889889
# impure_safe decorator:
@@ -894,7 +894,8 @@ def lash(self, function):
894894
@overload
895895
def impure_safe(
896896
function: Callable[_FuncParams, _NewValueType],
897-
) -> Callable[_FuncParams, IOResultE[_NewValueType]]:
897+
/,
898+
) -> Callable[_FuncParams, IOResult[_NewValueType, Exception]]:
898899
"""Decorator to convert exception-throwing for any kind of Exception."""
899900

900901

@@ -908,11 +909,13 @@ def impure_safe(
908909
"""Decorator to convert exception-throwing just for a set of Exceptions."""
909910

910911

911-
def impure_safe( # type: ignore # noqa: WPS234, C901
912-
function: Optional[Callable[_FuncParams, _NewValueType]] = None,
913-
exceptions: Optional[Tuple[Type[_ExceptionType], ...]] = None,
912+
def impure_safe( # noqa: WPS234, C901
913+
exceptions: Union[
914+
Callable[_FuncParams, _NewValueType],
915+
Tuple[Type[_ExceptionType], ...],
916+
],
914917
) -> Union[
915-
Callable[_FuncParams, IOResultE[_NewValueType]],
918+
Callable[_FuncParams, IOResult[_NewValueType, Exception]],
916919
Callable[
917920
[Callable[_FuncParams, _NewValueType]],
918921
Callable[_FuncParams, IOResult[_NewValueType, _ExceptionType]],
@@ -961,19 +964,22 @@ def impure_safe( # type: ignore # noqa: WPS234, C901
961964
"""
962965
def factory(
963966
inner_function: Callable[_FuncParams, _NewValueType],
964-
inner_exceptions: Tuple[Type[Exception], ...],
965-
) -> Callable[_FuncParams, IOResultE[_NewValueType]]:
967+
inner_exceptions: Tuple[Type[_ExceptionType], ...],
968+
) -> Callable[_FuncParams, IOResult[_NewValueType, _ExceptionType]]:
966969
@wraps(inner_function)
967-
def decorator(*args: _FuncParams.args, **kwargs: _FuncParams.kwargs):
970+
def decorator(
971+
*args: _FuncParams.args,
972+
**kwargs: _FuncParams.kwargs,
973+
) -> IOResult[_NewValueType, _ExceptionType]:
968974
try:
969975
return IOSuccess(inner_function(*args, **kwargs))
970976
except inner_exceptions as exc:
971977
return IOFailure(exc)
972978
return decorator
973979

974-
if callable(function):
975-
return factory(function, exceptions or (Exception,))
976-
if isinstance(function, tuple):
977-
exceptions = function # type: ignore
978-
function = None
979-
return lambda function: factory(function, exceptions) # type: ignore
980+
if isinstance(exceptions, tuple):
981+
return lambda function: factory(function, exceptions)
982+
return factory(
983+
exceptions,
984+
(Exception,), # type: ignore[arg-type]
985+
)

returns/result.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
overload,
1818
)
1919

20-
from typing_extensions import Never, ParamSpec
20+
from typing_extensions import Never, ParamSpec, TypeAlias
2121

2222
from returns.interfaces.specific import result
2323
from returns.primitives.container import BaseContainer, container_equality
@@ -468,8 +468,8 @@ def failure(self) -> Never:
468468

469469
# Aliases:
470470

471-
#: Alias for a popular case when ``Result`` has ``Exception`` as error type.
472-
ResultE = Result[_ValueType, Exception]
471+
#: Deprecated alias for ``Result[_ValueType, Exception]``.
472+
ResultE: TypeAlias = Result[_ValueType, Exception]
473473

474474

475475
# Decorators:
@@ -480,7 +480,8 @@ def failure(self) -> Never:
480480
@overload
481481
def safe(
482482
function: Callable[_FuncParams, _ValueType],
483-
) -> Callable[_FuncParams, ResultE[_ValueType]]:
483+
/,
484+
) -> Callable[_FuncParams, Result[_ValueType, Exception]]:
484485
"""Decorator to convert exception-throwing for any kind of Exception."""
485486

486487

@@ -494,11 +495,13 @@ def safe(
494495
"""Decorator to convert exception-throwing just for a set of Exceptions."""
495496

496497

497-
def safe( # type: ignore # noqa: WPS234, C901
498-
function: Optional[Callable[_FuncParams, _ValueType]] = None,
499-
exceptions: Optional[Tuple[Type[_ExceptionType], ...]] = None,
498+
def safe( # noqa: WPS234, C901
499+
exceptions: Union[
500+
Callable[_FuncParams, _ValueType],
501+
Tuple[Type[_ExceptionType], ...],
502+
],
500503
) -> Union[
501-
Callable[_FuncParams, ResultE[_ValueType]],
504+
Callable[_FuncParams, Result[_ValueType, Exception]],
502505
Callable[
503506
[Callable[_FuncParams, _ValueType]],
504507
Callable[_FuncParams, Result[_ValueType, _ExceptionType]],
@@ -546,22 +549,25 @@ def safe( # type: ignore # noqa: WPS234, C901
546549
"""
547550
def factory(
548551
inner_function: Callable[_FuncParams, _ValueType],
549-
inner_exceptions: Tuple[Type[Exception], ...],
550-
) -> Callable[_FuncParams, ResultE[_ValueType]]:
552+
inner_exceptions: Tuple[Type[_ExceptionType], ...],
553+
) -> Callable[_FuncParams, Result[_ValueType, _ExceptionType]]:
551554
@wraps(inner_function)
552-
def decorator(*args: _FuncParams.args, **kwargs: _FuncParams.kwargs):
555+
def decorator(
556+
*args: _FuncParams.args,
557+
**kwargs: _FuncParams.kwargs,
558+
) -> Result[_ValueType, _ExceptionType]:
553559
try:
554560
return Success(inner_function(*args, **kwargs))
555561
except inner_exceptions as exc:
556562
return Failure(exc)
557563
return decorator
558564

559-
if callable(function):
560-
return factory(function, (Exception,))
561-
if isinstance(function, tuple):
562-
exceptions = function # type: ignore
563-
function = None
564-
return lambda function: factory(function, exceptions) # type: ignore
565+
if isinstance(exceptions, tuple):
566+
return lambda function: factory(function, exceptions)
567+
return factory(
568+
exceptions,
569+
(Exception,), # type: ignore[arg-type]
570+
)
565571

566572

567573
def attempt(

0 commit comments

Comments
 (0)