Add unwrap_opt

This commit is contained in:
Emi Simpson 2023-02-11 23:59:05 -05:00
parent c3e5146029
commit 96e7a51ccc
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 22 additions and 0 deletions

View File

@ -546,6 +546,28 @@ def note(e: B, o: Option[A]) -> 'Result[A, B]':
case None:
return Err(e)
def unwrap_opt(r: Option[A]) -> A:
"""
Assert that an `Option` is `Some` and return it's value.
Throws:
`AssertionError` - The result was NOT okay. The `AssertionError` will have two
arguments: The first is a string to make it more obvious what happened. The
second is the error that was stored in the `Err`.
>>> unwrap_opt(Some('hai!'))
'hai!'
>>> unwrap_opt(None) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
AssertionError: ('Tried to unwrap a None value')
"""
match r:
case Some(val):
return val
case None:
raise AssertionError('Tried to unwrap a None value')
# Results!
@dataclass(frozen=True)
class Ok(Generic[A]):