Skip to content

Commit e444ea7

Browse files
authored
Add default_error parameter to converters.maybe_to_result (#1914)
1 parent 5b6c1fd commit e444ea7

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ See [0Ver](https://0ver.org/).
1414
- Improve inference of `ResultLike` objects when exception catching
1515
decorator is applied with explicit exception types
1616
- Add picky exceptions to `impure_safe` decorator like `safe` has. Issue #1543
17+
- Adds `default_error` parameter to `returns.converters.maybe_to_result`,
18+
which provides a default error value for `Failure`
1719

1820
### Misc
1921

docs/pages/converters.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ That's how they work:
2222
.. code:: python
2323
2424
>>> from returns.converters import maybe_to_result, result_to_maybe
25-
>>> from returns.maybe import Maybe, Some
26-
>>> from returns.result import Result, Success
25+
>>> from returns.maybe import Maybe, Some, Nothing
26+
>>> from returns.result import Failure, Result, Success
2727
2828
>>> result: Result[int, Exception] = Success(1)
2929
>>> maybe: Maybe[int] = result_to_maybe(result)
@@ -32,6 +32,9 @@ That's how they work:
3232
>>> new_result: Result[int, None] = maybe_to_result(maybe)
3333
>>> assert new_result == Success(1)
3434
35+
>>> failure_with_default: Result[int, str] = maybe_to_result(Nothing, 'abc')
36+
>>> assert failure_with_default == Failure('abc')
37+
3538
Take a note, that type changes.
3639
Also, take a note that ``Success(None)`` will be converted to ``Nothing``.
3740

returns/converters.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypeVar
1+
from typing import TypeVar, Union, overload
22

33
from returns.functions import identity
44
from returns.interfaces.bindable import BindableN
@@ -71,12 +71,30 @@ def result_to_maybe(
7171
return Nothing
7272

7373

74+
@overload
7475
def maybe_to_result(
7576
maybe_container: Maybe[_FirstType],
7677
) -> Result[_FirstType, None]:
78+
"""No default case."""
79+
80+
81+
@overload
82+
def maybe_to_result(
83+
maybe_container: Maybe[_FirstType],
84+
default_error: _SecondType,
85+
) -> Result[_FirstType, _SecondType]:
86+
"""Default value case."""
87+
88+
89+
def maybe_to_result(
90+
maybe_container: Maybe[_FirstType],
91+
default_error: Union[_SecondType, None] = None,
92+
) -> Result[_FirstType, Union[_SecondType, None]]:
7793
"""
7894
Converts ``Maybe`` container to ``Result`` container.
7995
96+
With optional ``default_error`` to be used for ``Failure``'s error value.
97+
8098
.. code:: python
8199
82100
>>> from returns.maybe import Some, Nothing
@@ -86,7 +104,9 @@ def maybe_to_result(
86104
>>> assert maybe_to_result(Some(None)) == Success(None)
87105
>>> assert maybe_to_result(Nothing) == Failure(None)
88106
107+
>>> assert maybe_to_result(Nothing, 'error') == Failure('error')
108+
89109
"""
90110
if is_successful(maybe_container):
91111
return Success(maybe_container.unwrap())
92-
return Failure(None)
112+
return Failure(default_error)

typesafety/test_converters/test_maybe_to_result.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
from returns.maybe import Maybe
66
77
reveal_type(maybe_to_result(Maybe.from_value(1))) # N: Revealed type is "returns.result.Result[builtins.int, None]"
8+
9+
10+
- case: maybe_to_result_default_error
11+
disable_cache: false
12+
main: |
13+
from returns.converters import maybe_to_result
14+
from returns.maybe import Maybe
15+
16+
reveal_type(maybe_to_result(Maybe.from_value(1), 'a')) # N: Revealed type is "returns.result.Result[builtins.int, builtins.str]"

0 commit comments

Comments
 (0)