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

Class Some

returns/maybe.py:390–452  ·  view source on GitHub ↗

Represents a calculation which has succeeded and contains the value. Quite similar to ``Success`` type.

Source from the content-addressed store, hash-verified

388
389@final
390class Some(Maybe[_ValueType_co]):
391 """
392 Represents a calculation which has succeeded and contains the value.
393
394 Quite similar to ``Success`` type.
395 """
396
397 __slots__ = ()
398
399 _inner_value: _ValueType_co
400
401 def __init__(self, inner_value: _ValueType_co) -> None:
402 """Some constructor."""
403 super().__init__(inner_value)
404
405 if not TYPE_CHECKING: # noqa: WPS604 # pragma: no branch
406
407 def bind(self, function):
408 """Binds current container to a function that returns container."""
409 return function(self._inner_value)
410
411 def bind_optional(self, function):
412 """Binds a function returning an optional value over a container."""
413 return Maybe.from_optional(function(self._inner_value))
414
415 def unwrap(self):
416 """Returns inner value for successful container."""
417 return self._inner_value
418
419 def map(self, function):
420 """Composes current container with a pure function."""
421 return Some(function(self._inner_value))
422
423 def apply(self, container):
424 """Calls a wrapped function in a container on this container."""
425 if isinstance(container, Some):
426 return self.map(container.unwrap()) # type: ignore
427 return container
428
429 def lash(self, function):
430 """Does nothing for ``Some``."""
431 return self
432
433 def value_or(self, default_value):
434 """Returns inner value for successful container."""
435 return self._inner_value
436
437 def or_else_call(self, function):
438 """Returns inner value for successful container."""
439 return self._inner_value
440
441 def failure(self):
442 """Raises exception for successful container."""
443 raise UnwrapFailedError(self)
444
445 def __bool__(self):
446 """
447 Returns ``True```.

Callers 15

result_to_maybeFunction · 0.90
_find_bookFunction · 0.90
test_some_is_trueFunction · 0.90
test_some_none_is_trueFunction · 0.90
test_unwrap_successFunction · 0.90
factoryFunction · 0.90
test_bind_someFunction · 0.90
test_bind_optionalFunction · 0.90
test_equalsFunction · 0.90
test_not_equalsFunction · 0.90
test_equalityFunction · 0.90
test_nonequalityFunction · 0.90

Calls

no outgoing calls

Tested by 15

_find_bookFunction · 0.72
test_some_is_trueFunction · 0.72
test_some_none_is_trueFunction · 0.72
test_unwrap_successFunction · 0.72
factoryFunction · 0.72
test_bind_someFunction · 0.72
test_bind_optionalFunction · 0.72
test_equalsFunction · 0.72
test_not_equalsFunction · 0.72
test_equalityFunction · 0.72
test_nonequalityFunction · 0.72
test_is_compareFunction · 0.72