Bases: Result[ValueType, ErrorType]
Represents a successful result, holding a value of type ValueType.
Source code in src/forging_blocks/foundation/result/ok.py
| class Ok[ValueType, ErrorType](Result[ValueType, ErrorType]):
"""Represents a successful result, holding a value of type ``ValueType``."""
__match_args__ = ("_value",)
def __init__(self, value: ValueType) -> None:
"""Wrap ``value`` as a successful `Result`."""
self._value = value
def __repr__(self) -> str:
"""Return a debug-friendly repr like ``Ok(value)``."""
return f"Ok({self._value!r})"
def __str__(self) -> str:
"""Return a user-friendly string like ``Ok(value)``."""
return f"Ok({self._value})"
def __eq__(self, other: object) -> bool:
"""Two Oks are equal when their wrapped values are equal."""
if not isinstance(other, Ok):
return False
other_ok: OkType = other # type: ignore[assignment]
return self._value == other_ok._value
def __hash__(self) -> int:
"""Hash based on the wrapped value."""
return hash(self._value)
@property
def is_ok(self) -> bool:
"""Always ``True`` — this variant holds a success value."""
return True
@property
def is_err(self) -> bool:
"""Always ``False`` — this variant does not hold an error."""
return False
@property
def value(self) -> ValueType:
"""The wrapped success value."""
return self._value
@property
def error(self) -> ErrorType:
"""Raises `ResultAccessError` — there is no error to access."""
raise ResultAccessError.cannot_access_error()
def map[MappedValueType](
self, fn: Callable[[ValueType], MappedValueType]
) -> Result[MappedValueType, ErrorType]:
"""Apply ``fn`` to the wrapped value and wrap the result in a new Ok."""
return Ok(fn(self._value))
def map_error[MappedErrorType](
self, fn: Callable[[ErrorType], MappedErrorType]
) -> Result[ValueType, MappedErrorType]:
"""Pass through unchanged — there is no error to transform."""
return Ok(self._value)
def flat_map[MappedValueType](
self, fn: Callable[[ValueType], Result[MappedValueType, ErrorType]]
) -> Result[MappedValueType, ErrorType]:
"""Apply ``fn`` to the wrapped value and return its Result directly."""
return fn(self._value)
def get_value_or(self, default: ValueType) -> ValueType:
"""Return the wrapped value, ignoring ``default``."""
return self._value
def get_value_or_else(self, fn: Callable[[ErrorType], ValueType]) -> ValueType:
"""Return the wrapped value, ignoring ``fn``."""
return self._value
|
is_ok: bool
property
Always True — this variant holds a success value.
is_err: bool
property
Always False — this variant does not hold an error.
value: ValueType
property
The wrapped success value.
error: ErrorType
property
Raises ResultAccessError — there is no error to access.
__init__(value: ValueType) -> None
Wrap value as a successful Result.
Source code in src/forging_blocks/foundation/result/ok.py
| def __init__(self, value: ValueType) -> None:
"""Wrap ``value`` as a successful `Result`."""
self._value = value
|
__repr__() -> str
Return a debug-friendly repr like Ok(value).
Source code in src/forging_blocks/foundation/result/ok.py
| def __repr__(self) -> str:
"""Return a debug-friendly repr like ``Ok(value)``."""
return f"Ok({self._value!r})"
|
__str__() -> str
Return a user-friendly string like Ok(value).
Source code in src/forging_blocks/foundation/result/ok.py
| def __str__(self) -> str:
"""Return a user-friendly string like ``Ok(value)``."""
return f"Ok({self._value})"
|
__eq__(other: object) -> bool
Two Oks are equal when their wrapped values are equal.
Source code in src/forging_blocks/foundation/result/ok.py
| def __eq__(self, other: object) -> bool:
"""Two Oks are equal when their wrapped values are equal."""
if not isinstance(other, Ok):
return False
other_ok: OkType = other # type: ignore[assignment]
return self._value == other_ok._value
|
__hash__() -> int
Hash based on the wrapped value.
Source code in src/forging_blocks/foundation/result/ok.py
| def __hash__(self) -> int:
"""Hash based on the wrapped value."""
return hash(self._value)
|
map(fn: Callable[[ValueType], MappedValueType]) -> Result[MappedValueType, ErrorType]
Apply fn to the wrapped value and wrap the result in a new Ok.
Source code in src/forging_blocks/foundation/result/ok.py
| def map[MappedValueType](
self, fn: Callable[[ValueType], MappedValueType]
) -> Result[MappedValueType, ErrorType]:
"""Apply ``fn`` to the wrapped value and wrap the result in a new Ok."""
return Ok(fn(self._value))
|
map_error(fn: Callable[[ErrorType], MappedErrorType]) -> Result[ValueType, MappedErrorType]
Pass through unchanged — there is no error to transform.
Source code in src/forging_blocks/foundation/result/ok.py
| def map_error[MappedErrorType](
self, fn: Callable[[ErrorType], MappedErrorType]
) -> Result[ValueType, MappedErrorType]:
"""Pass through unchanged — there is no error to transform."""
return Ok(self._value)
|
flat_map(fn: Callable[[ValueType], Result[MappedValueType, ErrorType]]) -> Result[MappedValueType, ErrorType]
Apply fn to the wrapped value and return its Result directly.
Source code in src/forging_blocks/foundation/result/ok.py
| def flat_map[MappedValueType](
self, fn: Callable[[ValueType], Result[MappedValueType, ErrorType]]
) -> Result[MappedValueType, ErrorType]:
"""Apply ``fn`` to the wrapped value and return its Result directly."""
return fn(self._value)
|
get_value_or(default: ValueType) -> ValueType
Return the wrapped value, ignoring default.
Source code in src/forging_blocks/foundation/result/ok.py
| def get_value_or(self, default: ValueType) -> ValueType:
"""Return the wrapped value, ignoring ``default``."""
return self._value
|
get_value_or_else(fn: Callable[[ErrorType], ValueType]) -> ValueType
Return the wrapped value, ignoring fn.
Source code in src/forging_blocks/foundation/result/ok.py
| def get_value_or_else(self, fn: Callable[[ErrorType], ValueType]) -> ValueType:
"""Return the wrapped value, ignoring ``fn``."""
return self._value
|