Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unwrap_or_raise_itself() if a value of Err is a BaseException #199

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions docs/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,19 @@ unwrap_or_raise(e: 'object') → T
Return the value.


---

<a href="https://github.com/rustedpy/result/blob/main/src/result/result.py#L143"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>method</kbd> `unwrap_or_raise_itself`

```python
unwrap_or_raise_itself() → T
```

Return the value.


---

<a href="https://github.com/rustedpy/result/blob/main/src/result/result.py#L221"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
Expand Down Expand Up @@ -789,6 +802,21 @@ unwrap_or_raise(e: 'Type[TBE]') → NoReturn
The contained result is ``Err``, so raise the exception with the value.


---

<a href="https://github.com/rustedpy/result/blob/main/src/result/result.py#L355"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>method</kbd> `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`.


---

<a href="https://github.com/rustedpy/result/blob/main/src/result/result.py#L442"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
Expand Down
20 changes: 20 additions & 0 deletions src/result/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -358,6 +364,20 @@ 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 the error is not an exception, this will raise an `UnwrapError`.
"""
if isinstance(self._value, BaseException):
raise self._value

raise UnwrapError(
self,
f"Called `Result.unwrap_or_raise_itself()` on non-exception value: {self._value}",
)
Comment on lines +376 to +379
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is preferred to raise, UnwrapError, TypeError or ValueError?


def map(self, op: object) -> Err[E]:
"""
Return `Err` with the same value
Expand Down
12 changes: 12 additions & 0 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down