Represents a calculation which has failed. It should contain an error code or message.
| 345 | |
| 346 | @final # noqa: WPS338 |
| 347 | class 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 |
no outgoing calls