Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ See [0Ver](https://0ver.org/).
- Add `default_error` parameter to `returns.converters.maybe_to_result`,
which provides a default error value for `Failure`

### 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

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,37 @@
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."""
try:
Nothing.unwrap() # This will raise UnwrapFailedError
except UnwrapFailedError as error:
# Serialize the error
serialized = pickle.dumps(error)

# 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)
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
# Serialize the error
serialized = pickle.dumps(error)
# 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)
# Serialize the error
serialized = pickle.dumps(error)
# 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)

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.

Please, remove the testing logic from except



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

# 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')