Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Copy Markdown
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,48 @@
import pickle # noqa: S403

from returns.maybe import Nothing, Some
from returns.primitives.exceptions import UnwrapFailedError
from returns.result import Failure, Success


def test_pickle_unwrap_failed_error_from_maybe():
"""Ensures that UnwrapFailedError with Maybe can be pickled."""
error = None
serialized = None
error = None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
error = None
serialized = None
error = None
serialized = None

We only need this var :)

try:
Nothing.unwrap() # This will raise UnwrapFailedError
except UnwrapFailedError as error:
# Serialize the error
serialized = pickle.dumps(error)

assert error is not None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
assert error is not None

assert serialized is not None

# Deserialize
deserialized_error = pickle.loads(serialized) # noqa: S301

# Check that halted_container is preserved
assert deserialized_error.halted_container == Nothing
assert deserialized_error.halted_container != Some(None)


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

assert error is not None
assert serialized is not None

# Deserialize
deserialized_error = pickle.loads(serialized) # noqa: S301
# Check that halted_container is preserved
assert deserialized_error.halted_container == Failure('error')
assert deserialized_error.halted_container != Success('error')