Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ See [0Ver](https://0ver.org/).
which provides a default error value for `Failure`


## 0.24.1

### Bugfixes
Copy link
Member

Choose a reason for hiding this comment

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

This should be 0.24.1


- Add pickling support for `UnwrapFailedError` exception


## 0.23.0

### Features
Expand Down
10 changes: 10 additions & 0 deletions returns/primitives/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def __init__(self, container: Unwrappable) -> None:
super().__init__()
self.halted_container = container

def __reduce__(self): # noqa: WPS603
"""Custom reduce method for pickle protocol.

This helps properly reconstruct the exception during unpickling.
"""
return (
self.__class__, # callable
(self.halted_container,), # args to callable
)


class ImmutableStateError(AttributeError):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pickle # noqa: S403

from returns.maybe import Nothing
from returns.primitives.exceptions import UnwrapFailedError
from returns.result import Failure


def test_pickle_unwrap_failed_error_from_maybe():
"""Ensures that UnwrapFailedError with Maybe can be pickled."""
serialized = None
try:
Nothing.unwrap() # This will raise UnwrapFailedError
except UnwrapFailedError as error:
serialized = pickle.dumps(error)

deserialized_error = pickle.loads(serialized) # noqa: S301
assert deserialized_error.halted_container == Nothing


def test_pickle_unwrap_failed_error_from_result():
"""Ensures that UnwrapFailedError with Result can be pickled."""
serialized = None
try:
Failure('error').unwrap() # This will raise UnwrapFailedError
except UnwrapFailedError as error:
serialized = pickle.dumps(error)

deserialized_error = pickle.loads(serialized) # noqa: S301
assert deserialized_error.halted_container == Failure('error')