Add unwrap_opt
This commit is contained in:
parent
c3e5146029
commit
96e7a51ccc
|
@ -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]):
|
||||
|
|
Loading…
Reference in New Issue