Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_error parameter to converters.maybe_to_result #1914

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]"
Loading