From 432e6ad55a9dc0e845dc33426a331fd412ec4494 Mon Sep 17 00:00:00 2001 From: Colk-tech Date: Tue, 29 Oct 2024 02:28:02 +0900 Subject: [PATCH 1/6] feat: add instance methods, `unwrap_or_raise_itself()` --- src/result/result.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/result/result.py b/src/result/result.py index 8551239..0ef10ab 100644 --- a/src/result/result.py +++ b/src/result/result.py @@ -146,6 +146,12 @@ def unwrap_or_raise(self, e: object) -> T: """ return self._value + def unwrap_or_raise_itself(self) -> T: + """ + Return the value. + """ + return self._value + def map(self, op: Callable[[T], U]) -> Ok[U]: """ The contained result is `Ok`, so return `Ok` with original value mapped to @@ -358,6 +364,17 @@ def unwrap_or_raise(self, e: Type[TBE]) -> NoReturn: """ raise e(self._value) + def unwrap_or_raise_itself(self) -> NoReturn: + """ + The contained result is ``Err``, so raise the error itself. + """ + if isinstance(self._value, BaseException): + raise self._value + + raise TypeError( + f"Called `Result.unwrap_or_raise_itself()` on non-exception value: {self._value}" + ) + def map(self, op: object) -> Err[E]: """ Return `Err` with the same value From 2b421fab505df13adfeb8e4e95d0e4e61d076a70 Mon Sep 17 00:00:00 2001 From: Colk-tech Date: Tue, 29 Oct 2024 02:36:42 +0900 Subject: [PATCH 2/6] fix: raise `UnwrapError()` instead of `TypeError()` --- src/result/result.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/result/result.py b/src/result/result.py index 0ef10ab..c28048f 100644 --- a/src/result/result.py +++ b/src/result/result.py @@ -371,8 +371,9 @@ def unwrap_or_raise_itself(self) -> NoReturn: if isinstance(self._value, BaseException): raise self._value - raise TypeError( - f"Called `Result.unwrap_or_raise_itself()` on non-exception value: {self._value}" + raise UnwrapError( + self, + f"Called `Result.unwrap_or_raise_itself()` on non-exception value: {self._value}", ) def map(self, op: object) -> Err[E]: From ce20ba152462703edb4b9133f6d2dc83fa08d181 Mon Sep 17 00:00:00 2001 From: Colk-tech Date: Tue, 29 Oct 2024 02:38:22 +0900 Subject: [PATCH 3/6] feat: add a test for `unwrap_or_raise_itself()` --- tests/test_result.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_result.py b/tests/test_result.py index c99c241..dd12aae 100644 --- a/tests/test_result.py +++ b/tests/test_result.py @@ -158,6 +158,18 @@ def test_unwrap_or_raise() -> None: assert exc_info.value.args == ('nay',) +def test_unwrap_or_raise_itself() -> None: + o = Ok('yay') + n = Err(RuntimeError('nay')) + assert o.unwrap_or_raise_itself() == 'yay' + with pytest.raises(RuntimeError) as exc_info: + n.unwrap_or_raise_itself() + assert exc_info.value.args == ('nay',) + ne = Err('nay') + with pytest.raises(UnwrapError): + ne.unwrap_or_raise_itself() + + def test_map() -> None: o = Ok('yay') n = Err('nay') From 5e33d330ecd8ecdecc00554ff1debc7532eeff35 Mon Sep 17 00:00:00 2001 From: Colk-tech Date: Tue, 29 Oct 2024 02:44:04 +0900 Subject: [PATCH 4/6] feat: add a sentence in a docstring of `Err().unwrap_or_raise_itself()` --- src/result/result.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/result/result.py b/src/result/result.py index c28048f..80d19e8 100644 --- a/src/result/result.py +++ b/src/result/result.py @@ -367,6 +367,8 @@ def unwrap_or_raise(self, e: Type[TBE]) -> NoReturn: def unwrap_or_raise_itself(self) -> NoReturn: """ The contained result is ``Err``, so raise the error itself. + + If the error is not an exception, this will raise an `UnwrapError`. """ if isinstance(self._value, BaseException): raise self._value From 2cd9b9ca23e03d42870a923a9b5703dc0be25f6a Mon Sep 17 00:00:00 2001 From: Colk-tech Date: Tue, 29 Oct 2024 02:50:18 +0900 Subject: [PATCH 5/6] feat: add entries about `unwrap_or_raise_itself()` in the doc --- docs/result.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/result.md b/docs/result.md index eb6d5ed..57102f1 100644 --- a/docs/result.md +++ b/docs/result.md @@ -473,6 +473,19 @@ unwrap_or_raise(e: 'object') → T Return the value. +--- + + + +### method `unwrap_or_raise_itself` + +```python +unwrap_or_raise_itself() → T +``` + +Return the value. + + --- @@ -789,6 +802,21 @@ unwrap_or_raise(e: 'Type[TBE]') → NoReturn The contained result is ``Err``, so raise the exception with the value. +--- + + + +### method `unwrap_or_raise_itself` + +```python +unwrap_or_raise_itself() → NoReturn +``` + +The contained result is ``Err``, so raise the error itself. + +If the error is not an exception, this will raise an `UnwrapError`. + + --- From d1213fafeee502be2b064bb914f8ae7ddf22408c Mon Sep 17 00:00:00 2001 From: Colk-tech Date: Tue, 29 Oct 2024 03:06:43 +0900 Subject: [PATCH 6/6] feat: add an entry in `CHANGELOG.md` for `unwrap_or_raise_itself()` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73f5d08..39b11db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ Possible log types: - `[changed]` Improve type narrowing for `is_ok` and `is_err` type guards by replacing `typing.TypeGuard` with `typing.TypeIs` (#193) +- `[added]` Add `unwrap_or_raise_itself()` for `Ok` and `Err` (#199) + ## [0.17.0] - 2024-06-02 - `[added]` Add `inspect()` and `inspect_err()` methods (#185)