Skip to content

Commit

Permalink
Add default_error parameter to converters.maybe_to_result (#1914)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Aug 14, 2024
1 parent 5b6c1fd commit e444ea7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See [0Ver](https://0ver.org/).
- Improve inference of `ResultLike` objects when exception catching
decorator is applied with explicit exception types
- Add picky exceptions to `impure_safe` decorator like `safe` has. Issue #1543
- Adds `default_error` parameter to `returns.converters.maybe_to_result`,
which provides a default error value for `Failure`

### Misc

Expand Down
7 changes: 5 additions & 2 deletions docs/pages/converters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ That's how they work:
.. code:: python
>>> from returns.converters import maybe_to_result, result_to_maybe
>>> from returns.maybe import Maybe, Some
>>> from returns.result import Result, Success
>>> from returns.maybe import Maybe, Some, Nothing
>>> from returns.result import Failure, Result, Success
>>> result: Result[int, Exception] = Success(1)
>>> maybe: Maybe[int] = result_to_maybe(result)
Expand All @@ -32,6 +32,9 @@ That's how they work:
>>> new_result: Result[int, None] = maybe_to_result(maybe)
>>> assert new_result == Success(1)
>>> failure_with_default: Result[int, str] = maybe_to_result(Nothing, 'abc')
>>> assert failure_with_default == Failure('abc')
Take a note, that type changes.
Also, take a note that ``Success(None)`` will be converted to ``Nothing``.

Expand Down
24 changes: 22 additions & 2 deletions returns/converters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar
from typing import TypeVar, Union, overload

from returns.functions import identity
from returns.interfaces.bindable import BindableN
Expand Down Expand Up @@ -71,12 +71,30 @@ def result_to_maybe(
return Nothing


@overload
def maybe_to_result(
maybe_container: Maybe[_FirstType],
) -> Result[_FirstType, None]:
"""No default case."""


@overload
def maybe_to_result(
maybe_container: Maybe[_FirstType],
default_error: _SecondType,
) -> Result[_FirstType, _SecondType]:
"""Default value case."""


def maybe_to_result(
maybe_container: Maybe[_FirstType],
default_error: Union[_SecondType, None] = None,
) -> Result[_FirstType, Union[_SecondType, None]]:
"""
Converts ``Maybe`` container to ``Result`` container.
With optional ``default_error`` to be used for ``Failure``'s error value.
.. code:: python
>>> from returns.maybe import Some, Nothing
Expand All @@ -86,7 +104,9 @@ def maybe_to_result(
>>> assert maybe_to_result(Some(None)) == Success(None)
>>> assert maybe_to_result(Nothing) == Failure(None)
>>> assert maybe_to_result(Nothing, 'error') == Failure('error')
"""
if is_successful(maybe_container):
return Success(maybe_container.unwrap())
return Failure(None)
return Failure(default_error)
9 changes: 9 additions & 0 deletions typesafety/test_converters/test_maybe_to_result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
from returns.maybe import Maybe
reveal_type(maybe_to_result(Maybe.from_value(1))) # N: Revealed type is "returns.result.Result[builtins.int, None]"
- case: maybe_to_result_default_error
disable_cache: false
main: |
from returns.converters import maybe_to_result
from returns.maybe import Maybe
reveal_type(maybe_to_result(Maybe.from_value(1), 'a')) # N: Revealed type is "returns.result.Result[builtins.int, builtins.str]"

0 comments on commit e444ea7

Please sign in to comment.