MCPcopy Index your code
hub / github.com/dry-python/returns / Failure

Class Failure

returns/result.py:347–407  ·  view source on GitHub ↗

Represents a calculation which has failed. It should contain an error code or message.

Source from the content-addressed store, hash-verified

345
346@final # noqa: WPS338
347class Failure(Result[Any, _ErrorType_co]): # noqa: WPS338
348 """
349 Represents a calculation which has failed.
350
351 It should contain an error code or message.
352 """
353
354 __slots__ = ()
355
356 _inner_value: _ErrorType_co
357
358 def __init__(self, inner_value: _ErrorType_co) -> None:
359 """Failure constructor."""
360 super().__init__(inner_value)
361 object.__setattr__(self, '_trace', self._get_trace())
362
363 if not TYPE_CHECKING: # noqa: WPS604 # pragma: no branch
364
365 def alt(self, function):
366 """Composes failed container with a pure function to modify failure.""" # noqa: E501
367 return Failure(function(self._inner_value))
368
369 def map(self, function):
370 """Does nothing for ``Failure``."""
371 return self
372
373 def bind(self, function):
374 """Does nothing for ``Failure``."""
375 return self
376
377 #: Alias for `bind` method. Part of the `ResultBasedN` interface.
378 bind_result = bind
379
380 def lash(self, function):
381 """Composes this container with a function returning container."""
382 return function(self._inner_value)
383
384 def apply(self, container):
385 """Does nothing for ``Failure``."""
386 return self
387
388 def value_or(self, default_value):
389 """Returns default value for failed container."""
390 return default_value
391
392 def swap(self):
393 """Failures swap to :class:`Success`."""
394 return Success(self._inner_value)
395
396 def unwrap(self) -> Never:
397 """Raises an exception, since it does not have a value inside."""
398 if isinstance(self._inner_value, Exception):
399 raise UnwrapFailedError(self) from self._inner_value
400 raise UnwrapFailedError(self)
401
402 def failure(self) -> _ErrorType_co:
403 """Returns failed value."""
404 return self._inner_value

Callers 15

__init__Method · 0.90
from_failureMethod · 0.90
factoryFunction · 0.90
maybe_to_resultFunction · 0.90
from_failed_contextMethod · 0.90
from_failureMethod · 0.90
async_altFunction · 0.90
async_from_failureFunction · 0.90
test_unwrap_failureFunction · 0.90
test_failure_valueFunction · 0.90
factoryFunction · 0.90

Calls

no outgoing calls

Tested by 15

test_unwrap_failureFunction · 0.72
test_failure_valueFunction · 0.72
factoryFunction · 0.72
test_lash_failureFunction · 0.72
test_equalsFunction · 0.72
test_not_equalsFunction · 0.72
test_non_equalityFunction · 0.72
test_is_compareFunction · 0.72