|
2 | 2 |
|
3 | 3 | from functools import wraps |
4 | 4 | from inspect import iscoroutinefunction |
5 | | -from typing import Callable, Coroutine, TypeVar, Union, overload |
| 5 | +from typing import Callable, Coroutine, Type, TypeVar, Union, overload |
| 6 | + |
| 7 | +from typing_extensions import Protocol |
6 | 8 |
|
7 | 9 | from returns._generated.pipe import _pipe as pipe # noqa: F401, WPS436 |
8 | 10 | from returns.maybe import Maybe |
|
17 | 19 | _SecondType = TypeVar('_SecondType') |
18 | 20 |
|
19 | 21 | # Hacks for functions: |
20 | | -_ReturnsResultType = TypeVar( |
21 | | - '_ReturnsResultType', |
22 | | - bound=Callable[..., _Unwrapable], |
| 22 | +_ReturnResultType = TypeVar( |
| 23 | + '_ReturnResultType', |
| 24 | + bound=Callable[..., Result], |
23 | 25 | ) |
24 | | -_AsyncReturnsResultType = TypeVar( |
25 | | - '_AsyncReturnsResultType', |
26 | | - bound=Callable[..., Coroutine[_FirstType, _SecondType, _Unwrapable]], |
| 26 | +_AsyncReturnResultType = TypeVar( |
| 27 | + '_AsyncReturnResultType', |
| 28 | + bound=Callable[..., Coroutine[_FirstType, _SecondType, Result]], |
| 29 | +) |
| 30 | + |
| 31 | +_ReturnMaybeType = TypeVar( |
| 32 | + '_ReturnMaybeType', |
| 33 | + bound=Callable[..., Maybe], |
| 34 | +) |
| 35 | +_AsyncReturnMaybeType = TypeVar( |
| 36 | + '_AsyncReturnMaybeType', |
| 37 | + bound=Callable[..., Coroutine[_FirstType, _SecondType, Maybe]], |
27 | 38 | ) |
28 | 39 |
|
29 | 40 |
|
@@ -56,36 +67,83 @@ def is_successful(container: _Unwrapable) -> bool: |
56 | 67 | return True |
57 | 68 |
|
58 | 69 |
|
| 70 | +class _PipelineResultProtocol(Protocol): |
| 71 | + @overload |
| 72 | + def __call__(self, function: _ReturnResultType) -> _ReturnResultType: |
| 73 | + """Sync pipeline case for ``Result`` container.""" |
| 74 | + |
| 75 | + @overload # noqa: F811 |
| 76 | + def __call__( |
| 77 | + self, function: _AsyncReturnResultType, |
| 78 | + ) -> _AsyncReturnResultType: |
| 79 | + """Async pipeline case for ``Result`` container.""" |
| 80 | + |
| 81 | + |
| 82 | +class _PipelineMaybeProtocol(Protocol): |
| 83 | + @overload |
| 84 | + def __call__(self, function: _ReturnMaybeType) -> _ReturnMaybeType: |
| 85 | + """Sync pipeline case for ``Maybe`` container.""" |
| 86 | + |
| 87 | + @overload # noqa: F811 |
| 88 | + def __call__( |
| 89 | + self, function: _AsyncReturnMaybeType, |
| 90 | + ) -> _AsyncReturnMaybeType: |
| 91 | + """Async pipeline case for ``Maybe`` container.""" |
| 92 | + |
| 93 | + |
59 | 94 | @overload |
60 | 95 | def pipeline( |
61 | | - function: _AsyncReturnsResultType, |
62 | | -) -> _AsyncReturnsResultType: |
63 | | - """Case for async functions.""" |
| 96 | + container_type: Type[Result], |
| 97 | +) -> _PipelineResultProtocol: |
| 98 | + """Pipeline case for ``Result`` container.""" |
64 | 99 |
|
65 | 100 |
|
66 | 101 | @overload |
67 | | -def pipeline(function: _ReturnsResultType) -> _ReturnsResultType: |
68 | | - """Case for regular functions.""" |
| 102 | +def pipeline( |
| 103 | + container_type: Type[Maybe], |
| 104 | +) -> _PipelineMaybeProtocol: |
| 105 | + """Pipeline case for ``Maybe`` container.""" |
69 | 106 |
|
70 | 107 |
|
71 | | -def pipeline(function): # noqa: C901 |
| 108 | +def pipeline(container_type): # noqa: C901, WPS212 |
72 | 109 | """ |
73 | 110 | Decorator to enable ``do-notation`` context. |
74 | 111 |
|
75 | 112 | Should be used for series of computations that rely on ``.unwrap`` method. |
76 | | -
|
77 | 113 | Supports both async and regular functions. |
| 114 | +
|
| 115 | + Works with both ``Maybe`` and ``Result`` containers. |
| 116 | +
|
| 117 | + Example: |
| 118 | + .. code:: python |
| 119 | +
|
| 120 | + >>> from typing import Optional |
| 121 | + >>> @pipeline(Maybe) |
| 122 | + ... def test(one: Optional[int], two: Optional[int]) -> Maybe[int]: |
| 123 | + ... first = Maybe.new(one).unwrap() |
| 124 | + ... second = Maybe.new(two).unwrap() |
| 125 | + ... return Maybe.new(first + second) |
| 126 | + ... |
| 127 | + >>> str(test(1, 2)) |
| 128 | + '<Some: 3>' |
| 129 | + >>> str(test(2, None)) |
| 130 | + '<Nothing>' |
| 131 | +
|
| 132 | + Make sure to supply the correct container type when creating a pipeline. |
| 133 | +
|
78 | 134 | """ |
79 | | - if iscoroutinefunction(function): |
80 | | - async def decorator(*args, **kwargs): # noqa: WPS430 |
81 | | - try: |
82 | | - return await function(*args, **kwargs) |
83 | | - except UnwrapFailedError as exc: |
84 | | - return exc.halted_container |
85 | | - else: |
86 | | - def decorator(*args, **kwargs): # noqa: WPS430 |
87 | | - try: |
88 | | - return function(*args, **kwargs) |
89 | | - except UnwrapFailedError as exc: |
90 | | - return exc.halted_container |
91 | | - return wraps(function)(decorator) |
| 135 | + def factory(function): |
| 136 | + if iscoroutinefunction(function): |
| 137 | + async def decorator(*args, **kwargs): # noqa: WPS430 |
| 138 | + try: |
| 139 | + return await function(*args, **kwargs) |
| 140 | + except UnwrapFailedError as exc: |
| 141 | + return exc.halted_container |
| 142 | + else: |
| 143 | + def decorator(*args, **kwargs): # noqa: WPS430 |
| 144 | + try: |
| 145 | + return function(*args, **kwargs) |
| 146 | + except UnwrapFailedError as exc: |
| 147 | + return exc.halted_container |
| 148 | + return wraps(function)(decorator) |
| 149 | + return factory |
0 commit comments