File: unwrap_or_failure.py

package info (click to toggle)
python-returns 0.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (27 lines) | stat: -rw-r--r-- 728 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import TypeVar

from returns.interfaces.unwrappable import Unwrappable
from returns.pipeline import is_successful

_FirstType = TypeVar('_FirstType')
_SecondType = TypeVar('_SecondType')


def unwrap_or_failure(
    container: Unwrappable[_FirstType, _SecondType],
) -> _FirstType | _SecondType:
    """
    Unwraps either successful or failed value.

    .. code:: python

      >>> from returns.io import IO, IOSuccess, IOFailure
      >>> from returns.methods import unwrap_or_failure

      >>> assert unwrap_or_failure(IOSuccess(1)) == IO(1)
      >>> assert unwrap_or_failure(IOFailure('a')) == IO('a')

    """
    if is_successful(container):
        return container.unwrap()
    return container.failure()