From 96e7a51cccea35f8f5906365759038a415f9bce6 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sat, 11 Feb 2023 23:59:05 -0500 Subject: [PATCH] Add unwrap_opt --- emis_funky_funktions.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/emis_funky_funktions.py b/emis_funky_funktions.py index 3e3fa5a..54a1706 100644 --- a/emis_funky_funktions.py +++ b/emis_funky_funktions.py @@ -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]):